0

I am trying to search for a substring with the specify pattern using re library. I wrote a function to do that but end up getting this error: Type Error: cannot concatenate str and integers. Below is my function;

def searchValue(obs, concept):
try:
    found = re.search('## !!'concept+'=(.+?)!! ##',obs)
except AttributeError:
    found = 'null'
return found

obs= '!!1834=7850!! ## !!1915=Patient is awaiting imaging results, then start darcabazine 250 ml every 21 days.!! ## !!1915=Patient is HIV positive since 2016,no presents with pains on the plantar surface and pelvic pain.!! ## !!5096=2013-07-29!! ## !!5219=1068!! ## !!6504=7189!! ## !!6509=6511!! ## !!6575=1107!! ## !!6605=1065!! ## !!7191=MELANOMA OF THE RIGHT HEEL.!! ## !!8723=5622!!'
bett = searchValue(obs, 1915)

print(bett)

Any help will be appreciated.

3
  • '## !!'concept+'=(.+?)!! ##' is not a correct string. Commented Apr 12, 2019 at 13:17
  • For this code you should get SyntaxError (missing + before concept). Commented Apr 12, 2019 at 13:17
  • As @CristiFati pointed out you are getting that error because you need to do '## !!'+concept+'=(.+?)!! ##'. Python is trying to concat "str1""str2" <-- valid python syntax. But in your case "str2" is an int which is why it errored. Commented Apr 12, 2019 at 13:46

7 Answers 7

1

If you are not interested in substrings position I'd use:

def searchValue(obs, concept):
    return re.findall('## !!'+ str(concept) + '=(.+?)!! ##',obs)

bett = searchValue(obs, 1915)
print(bett)

>>> ['Patient is awaiting imaging results, then start darcabazine 250 ml every 21 days.']


Sign up to request clarification or add additional context in comments.

Comments

1

after some wortk on your code i got something that seems fine; if not let me know.

The important part is to feed the re.search() with a complete string; that's done using .format().

Greetings


def searchValue(obs, concept):
    try:
        expression = '## !!{0}=(.+?)!! ##'.format(concept)
        found = re.search(expression, obs)
    except AttributeError:
        found = 'null'
    return found

obs= '!!1834=7850!! ## !!1915=Patient is awaiting imaging results, then start darcabazine 250 ml every 21 days.!! ## !!1915=Patient is HIV positive since 2016,no presents with pains on the plantar surface and pelvic pain.!! ## !!5096=2013-07-29!! ## !!5219=1068!! ## !!6504=7189!! ## !!6509=6511!! ## !!6575=1107!! ## !!6605=1065!! ## !!7191=MELANOMA OF THE RIGHT HEEL.!! ## !!8723=5622!!'
bett = searchValue(obs, 1915)

print(bett)

Comments

1

There are 2 errors in your code:

  1. Missing a + in '## !!'concept (might be a typo?) - yielding syntactically incorrect code (SyntaxError)
  2. Adding strings (`'## !!') with ints (1915) - which is not possible (TypeError). You have to convert the int to str

Here's how your pattern (re.search's 1st argument) should look like (quickest way, of course there's room for improvements):

>>> concept = 1915
>>> obs = '!!1834=7850!! ## !!1915=Patient is awaiting imaging results, then start darcabazine 250 ml every 21 days.!! ## !!1915=Patient is HIV positive since 2016,no presents with pains on the plantar surface and pelvic pain.!! ## !!5096=2013-07-29!! ## !!5219=1068!! ## !!6504=7189!! ## !!6509=6511!! ## !!6575=1107!! ## !!6605=1065!! ## !!7191=MELANOMA OF THE RIGHT HEEL.!! ## !!8723=5622!!'
>>>
>>> found = re.search('## !!' + str(concept) + '=(.+?)!! ##', obs)  # !!! Copy / paste this in your code
>>>
>>> found
<re.Match object; span=(14, 110), match='## !!1915=Patient is awaiting imaging results, th>
>>> found.group()
'## !!1915=Patient is awaiting imaging results, then start darcabazine 250 ml every 21 days.!! ##'

Comments

0

You are trying to concatenate a string with integer type that's why you are getting the error. try using this and do share if it solves your problem:

bett = searchValue(obs, str(1915))

also add the + sign before concept as suggested by @CristiFati

Comments

0

You missed a +. You also need to make the integer that you are trying to concatenate (concept) a string because you cannot concatenate integers with strings. You must also make the 1915 you are searching for in the 'obs' variable a string because 'obs' is a string not an integer.

def searchValue(obs, concept):
try:
    found = re.search('## !!' + str(concept) + '=(.+?)!! ##',obs)
except AttributeError:
    found = 'null'
return found

obs= '!!1834=7850!! ## !!1915=Patient is awaiting imaging results, then start darcabazine 250 ml every 21 days.!! ## !!1915=Patient is HIV positive since 2016,no presents with pains on the plantar surface and pelvic pain.!! ## !!5096=2013-07-29!! ## !!5219=1068!! ## !!6504=7189!! ## !!6509=6511!! ## !!6575=1107!! ## !!6605=1065!! ## !!7191=MELANOMA OF THE RIGHT HEEL.!! ## !!8723=5622!!'
bett = searchValue(obs, str(1915))

print(bett)

Comments

0

I think you mean:

    found = re.search('## !!' + concept + '=(.+?)!! ##',obs)

The correct version is:

    found = re.search('## !!' + str(concept) + '=(.+?)!! ##',obs)

You have to cast from int to string explicitly.

Comments

0

Your code is correct but you are missing a + for concat

def searchValue(obs, concept):
try:
    found = re.search('## !!'+str(concept)+'=(.+?)!! ##',obs)
except AttributeError:
    found = 'null'
return found

obs= '!!1834=7850!! ## !!1915=Patient is awaiting imaging results, then start darcabazine 250 ml every 21 days.!! ## !!1915=Patient is HIV positive since 2016,no presents with pains on the plantar surface and pelvic pain.!! ## !!5096=2013-07-29!! ## !!5219=1068!! ## !!6504=7189!! ## !!6509=6511!! ## !!6575=1107!! ## !!6605=1065!! ## !!7191=MELANOMA OF THE RIGHT HEEL.!! ## !!8723=5622!!'
bett = searchValue(obs, 1915)

print(bett)

Make sure you convert the integer value to string and pass so that it considers it as a string or convert it using str(Integer)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.