Regular Expression for Code
I am having a hard time finding a regular expression开发者_JAVA技巧 that can match these Strings:
@OSMHO:6:75
@4F0SO:5:56
@40KIR:5:15
@VDXBC:4:13
@WYRRA:6:59
@A4AUN:1118:803
As you guys can see these strings start with an '@' followed by 5 alphanumeric characters, then a ':', then a number, then another ':', then another number. Thanks so much for your help.
To match a @
at the beginning of a pattern, use ^
(beginning of line) followed by @
. So, your pattern begins with^@
.
Alphanumeric means any letter from a-z, A-Z and 0-9. When you want to represent "one of the following characters" in a regular expression the syntax is to enclose the set of characters in []
. In this case it would look like [a-zA-Z0-9]
. To say you want five of them you can use {5}
after the set of characters. Your expression now looks like ^@[a-zA-Z0-9]{5}
A colon is just a colon :
. A multi-digit number means you want one or more digits. A digit is represented as [0-9]
(ie: one of the numbers between 0 and 9). "one or more" is represented by +
. So, to add a colon, one-or-more digits, a colon and one-or-more digits you would add :[0-9]+:[0-9]+
. Your pattern now looks like this: ^@[a-zA-Z0-9]{5}:[0-9]+:[0-9]+
.
You can also use the shorthand \d
to mean "a digit", so you could also write^@[a-zA-Z0-9]{5}:\d+:\d+
, though that can be tricky because you might need extra backslashes depending on what sort of quotes you use to define that expression. Sometimes it's easiest to avoid shortcuts that use backslashes to make the pattern easier to understand, especially when you are first learning how to use regular expressions.
If you want to capture each part of that match in a group, you can use parenthesis. For example, you could do ^@([a-zA-Z0-9]{5}):([0-9]+):([0-9]+)
which will put the value between the @
and first :
in one group, the value between the two colons in a second group, and the value after the last colon in a third group. If you only care whether you have a match or not rather than wanting each individual piece of the match you can leave the parenthesis off.
If you build up a pattern in the way I just did -- tackling one piece at a time -- regular expressions can be very easy.
/@[\d\w]{5}:\d+:\d+/
\d is a digit, \w a character.
try
#@([a-zA-Z0-9]{5}):([0-9]+):([0-9]+)#i
Tried this in python:
>>> str = 'this is a test @WYRRA:6:59tyusd'
>>> match = re.search('\@[A-Z0-9]{5}\:[0-9]+\:[0-9]+',str)
>>> print match.group(0)
@WYRRA:6:59
So, the regex is:
- \@ == the @ sign escaped (not really needed to be escaped though)
- [A-Z0-9]{5} == exactly 5 occurences of any capital letter or numeric digit
- \: == the : sign escaped
- [0-9]+ == any numeric digit one or more times
The other answers seem correct. You might notice little differences in the regular expressions in each answer like the starting and ending '/' used in javascript.
精彩评论