简体   繁体   中英

How to Find and Output Regex Pattern between two strings in Python

I am trying to match all the times the a string starts with and ends with and then capture or print the value between the two ends, which would be in this case, 1112223333.

Here is my code so far that just prints none when I run it. I am relatively new to regex so all the help would be appreciated.:

string= "<x:a>1112223333</x:a>"
x = re.search(r"\<x:a/w+", str)
print(x)

I want the output to be:

'1112223333'

So another example, if the string was string= "3333333333", I would want the output to be

'3333333333'

Any ideas/suggestions?

Basically you use a group, which is defined by parenthesis, in (\w+), and then your reffer to it by its number, as in x.group(1). if you had 2 groups, than you understand the analogy of numbering the groups...

string= "<x:a>1112223333</x:a>"
import re
x = re.search(r"\<x:a\>(\w+)\<\/x:a\>", string)
print(x.group(1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM