191

I'm trying to parse a document that has reference numbers littered throughout it.

Text text text {4:2} more incredible text {4:3} much later on {222:115} and yet some more text.

The references will always be wrapped in brackets, and there will always be a colon between the two. I wrote an expression to find them.

{[0-9]:[0-9]} 

However, this obviously fails the moment you come across a two or three digit number, and I'm having trouble figuring out what that should be. There won't ever be more than 3 digits {999:999} is the maximum size to deal with.

Anybody have an idea of a proper expression for handling this?

0

5 Answers 5

195
{[0-9]+:[0-9]+} 

try adding plus(es)

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

Comments

111

What regex engine are you using? Most of them will support the following expression:

\{\d+:\d+\} 

The \d is actually shorthand for [0-9], but the important part is the addition of + which means "one or more".

Comments

44

Try this:

{[0-9]{1,3}:[0-9]{1,3}} 

The {1,3} means "match between 1 and 3 of the preceding characters".

Comments

13

You can specify how many times you want the previous item to match by using {min,max}.

{[0-9]{1,3}:[0-9]{1,3}} 

Also, you can use \d for digits instead of [0-9] for most regex flavors:

{\d{1,3}:\d{1,3}} 

You may also want to consider escaping the outer { and }, just to make it clear that they are not part of a repetition definition.

1 Comment

No please don't do it with most regex flavors, unless you love non-european digits: fileformat.info/info/unicode/category/Nd/list.htm
0

You could use this:

{[0-9]+:[0-9]+} 

or the shorthand version \d which matches any number from 0 to 9

{d+:d+} 

2 Comments

Isn't this exactly what the other, highly upvoted answers suggest? Explain what is different, or else risk this post to be deleted.
It is the same as the highly upvoted answer indeed, with the addition of the shorthand version with d

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.