def find_and_sep(inputstr, thing_to_find):
    #thing to find is a string that is a regular expression
    #finds thing_to_find and returns tuple of the rest (before, thing, after)
    inputstr.strip()
    #match it
    m=re.match(thing_to_find, inputstr) #match first instance
    if(m==None):
        return ""
    start = inputstr[:m.start()]
    mid = inputstr[m.start():m.end()]
    end = inputstr[m.end():]
    tuple_x = start,mid,end
    return tuple_x

my problem is when I give it

ins="blahCOMblah"
tmp=find_and_sep(ins, "(COM)?")

It only puts the whole thing in the last of the 3tuple

I want it to split into <blah,COM,blah>

Anyone know how to fix this?

Recommended Answers

All 4 Replies

Maybe this script solve your problem.

import re

def find_and_sep(inputstr, thing_to_find):
    #thing to find is a string that is a regular expression
    #finds thing_to_find and returns tuple of the rest (before, thing, after)
    #inputstr.strip() --> no need bro
    #match it
    m = re.search(thing_to_find, inputstr) # modified, match -> search
    if(m==None):
        return ""
    start = inputstr[:m.start()]
    mid = inputstr[m.start():m.end()]
    end = inputstr[m.end():]
    tuple_x = start,mid,end
    return tuple_x

ins = 'blahCOMblah'
a = find_and_sep(ins, 'COM')
print a

Help me out here?

I thought the only diff b/w match and search was match returns the first of those that match.

And I thought "blah" and 'blah' are the same in python.

Help me out here?

I thought the only diff b/w match and search was match returns the first of those that match.

And I thought "blah" and 'blah' are the same in python.

match and search different on where to check for a match. match checks for a match only at the beginning of the string. search checks for a match anywhere in the string.

agree. In python, there's no different b/w double-quote and single-quote. But I like using single-quote. :)

Member Avatar for sravan953

Try this:

ask=raw_input("Enter: ")
thing=raw_input("What do you want to find: ")

len1=len(thing)
n=0

t1=''
t2=''
t3=''

for x in ask:
    if(x==thing[0]):
        if(ask[n:n+len1]==thing):
            t1=ask[:n]
            t2=ask[n:n+len1]
            t3=ask[n+len1:]
            break
    n+=1

print(t1,t2,t3)
raw_input()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.