1

I am having a hard time finding a regular expression 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.

4
  • 1
    You're not going to find that exact regular expression anywhere.. but after a quick overview of regex syntax, you could write this yourself in about as much time as it took to write the question. I'm voting to close as too localized, as I can't imagine anyone else ever needing this exact regex. Commented May 16, 2011 at 19:06
  • @BlueRaja, while it's unlikely anyone else will need this exact regex, a good answer will explain how to write it, which is a lot more valuable than either the exact answer or a link to the syntax. Commented May 16, 2011 at 19:09
  • 1
    @eye: a good answer need not consist of more than "read the documentation", so it does not have any additional value to the documentation itself. Commented May 16, 2011 at 19:11
  • 1
    @Ether, "read the documentation" is never a good answer. Believe it or not, some people require more than a specification to learn something new, and explanation is valuable. If "read the documentation" were a good answer, there would be no purpose to a site like SO. Commented May 16, 2011 at 20:04

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much, I was having a hard time understanding reg ex
0

/@[\d\w]{5}:\d+:\d+/

\d is a digit, \w a character.

6 Comments

what do the '/' at the start and end of the reg ex mean? thanks
In most environments for regexes it delimits the start and end, if your particular library doesn't need those, you can leave them out.
I am using Java and it doesnt seem to like the escape characters
In that case you'd have to escape them again ("@[\\d\\w] etc.")
@tstenner: not "most", just "some".
|
0

try

#@([a-zA-Z0-9]{5}):([0-9]+):([0-9]+)#i

Comments

0

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.

2 Comments

why did you escape the colon and @? Don't you think that will be confusing to someone who is just starting to learn regular expressions?
Bryan, when I entered my answer there was no indication of programming language. So, I thought if I escaped it it would be safer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.