开发者

Using RegEx with something of the format "xxx:abc" to match just "abc"?

开发者 https://www.devze.com 2023-03-09 02:55 出处:网络
I\'ve not done much RegEx, so I\'m having issues coming up with a good RegEx for this script. Here are some example inputs:

I've not done much RegEx, so I'm having issues coming up with a good RegEx for this script.

Here are some example inputs:

document开发者_JAVA技巧:ASoi4jgt0w9efcZXNDOFzsdpfoasdf-zGRnae4iwn2, file:90jfa9_189204hsfiansdIASDNF, pdf:a09srjbZXMgf9oe90rfmasasgjm4-ab, spreadsheet:ASr0gk0jsdfPAsdfn

And here's what I'd want to match on each of those examples: ASoi4jgt0w9efcZXNDOFzsdpfoasdf-zGRnae4iwn2, 90jfa9_189204hsfiansdIASDNF, a09srjbZXMgf9oe90rfmasasgjm4-ab, ASr0gk0jsdfPAsdfn

What would be the best and perhaps simplest RegEx to use for this? Thanks!


.*:(.*) should get you everything after the last colon in the string as the value of the first group (or second group if you count the 'match everything' group).

An alternative would be [^:]*$ which gets you all characters at the end of the string up to but not including the last character in the string that is a colon.


Use something like below:

([^:]*)(,|$)

and get the first group. You can use a non-capturing group (?:ABC) if needed for the last. Also this makes the assumption that the value itself can have , as one of the characters.

I don't think answers like (.*)\:(.*) would work. It will match entire string.


(.*)\:(.*)

And take the second capture group...


Simplest seems to be [^:]*:([^,]*)(?:,|$).

That is find something that has something (possibly nothing) up to a colon, then a colon, then anything not including a comma (which is the thing matched), up to a comma or the end of the line.

Note the use of a non-capturing group at the end to encapsulate the alternation. The only capturing group appearing is the one which you wish to use.

So in python:

import re

exampStr = "document:ASoi4jgt0w9efcZXNDOFzsdpfoasdf-zGRnae4iwn2, file:90jfa9_189204hsfiansdIASDNF, pdf:a09srjbZXMgf9oe90rfmasasgjm4-ab, spreadsheet:ASr0gk0jsdfPAsdfn"

regex = re.compile("[^:]*:([^,]*)(?:,|$)")
result = regex.findall(exampStr)
print result

#
# Result:
#
#   ['ASoi4jgt0w9efcZXNDOFzsdpfoasdf-zGRnae4iwn2', '90jfa9_189204hsfiansdIASDNF', 'a09srjbZXMgf9oe90rfmasasgjm4-ab', 'ASr0gk0jsdfPAsdfn']
#
#

A good introduction is at: http://www.regular-expressions.info/tutorial.html .

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号