user2841795
user2841795

Reputation: 405

regular expression to get string value in python

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?

Upvotes: 0

Views: 68

Answers (1)

Dani Mesejo
Dani Mesejo

Reputation: 61910

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.

Upvotes: 1

Related Questions