简体   繁体   中英

Regular expression to match a specific pattern

I have the following string:

s = "<X> First <Y> Second"

and I can match any text right after <X> and <Y> (in this case "First" and "Second"). This is how I already did it:

import re
s = "<X> First <Y> Second"
pattern = r'\<([XxYy])\>([^\<]+)'  # lower and upper case X/Y will be matched
items = re.findall(pattern, s)
print items
>>> [('X', ' First '), ('Y', ' Second')]

What I am now trying to match is the case without <> :

s = "X First Y Second"

I tried this:

pattern = r'([XxYy]) ([^\<]+)'
>>> [('X', ' First Y Second')]

Unfortunately it's not producing the right result. What am I doing wrong? I want to match X or x or Y or y PLUS one whitespace (for instance "X "). How can I do that?

EDIT: this is a possible string too:

s = "<X> First one <Y> Second <X> More <Y> Text"

Output should be:

 >>> [('X', ' First one '), ('Y', ' Second '), ('X', ' More '), ('Y', ' Text')]

EDIT2:

pattern = r'([XxYy]) ([^ ]+)'
s = "X First text Y Second"

produces:

[('X', 'First'), ('Y', 'Second')]

but it should be:

[('X', 'First text'), ('Y', 'Second')]

How about something like: <?[XY]>? ([^<>XY$ ]+) <?[XY]>? ([^<>XY$ ]+)

Example in javascript:

 const re = /<?[XY]>? ([^<>XY$ ]+)/ig console.info('<X> First <Y> Second'.match(re)) console.info('X First Y Second'.match(re))

If you know which whitespace char to match, you can just add it to your expression. If you want any whitespace to match, you can use \\s

pattern = r'\<([XxYy])\>([^\<]+)'

would then be

pattern = r'\<([XxYy])\>\s([^\<]+)'

Always keep in mind the the expression within the () is what will be returned as your result.

假设要匹配的空白标记是单个空格字符,则模式为:

pattern = r'([XxYy]) ([^ ]+)'

所以我想出了这个解决方案:

pattern = r"([XxYy]) (.*?)(?= [XxYy] |$)"

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