0

I would like to get a string value from the given string using Regular Expression

Regular Expression:

(?i)\|HUMAN=(.?)|\|HUMAN=(.?)\|

String Value:

this is test results|HUMAN=Man|HM_LA=this is test results

I would like to get 'Man' as results. I have tried enough but not successful, can someone help please?

0

1 Answer 1

1

If you want to match any text after HUMAN= and before |, you could do:

import re
res = re.search(r'\|HUMAN=(\w+?)\|', 'this is test results|HUMAN=Man|HM_LA=this is test results')
print(res.group(1))

Output

Man

If you want to match only words, do:

res = re.search(r'\|HUMAN=(\w+?)\|', 'this is test results|HUMAN=Man|HM_LA=this is test results')
print(res.group(1))

Output

Man

The \w matches Unicode word characters and the + means one or more times. See here for a detailed explanation of the last regex, and here for an introduction to regular expressions in Python.

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

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.