tags:

views:

67

answers:

4

I would like to be able to extract a number from within a string formatted as follows:

"<[1085674730]> hello foo1, how are you doing?"

I'm a novice with regular expressions, I only want to be able to extract a number that is enclosed in the greater/less-than and bracket symbols, but I'm not sure how to go about it. I have to match numeric digits only, but I'm not sure what syntax is used for only searching within these symbols.

UPDATE:

Thank you all for you input, sorry for not being more specific, as I explained to kiamlaluno, I'm using VB.Net as the language for my application. I was wondering why some of the implementations were not working. In fact, the only one that did work was the one described by Matthew Flaschen. But that captures the symbols around the number as well as the number itself. I would like to only capture the number that is encased in the symbols and filter out the symbols themselves.

+3  A: 

Use:

<\[(\d+)\]>

This is tested with ECMAScript regex.

It means:

  • \[ - literal [
  • ( - open capturing group
  • \d - digit
  • + - one or more
  • ) - close capturing group
  • \] - literal ]

The overall functionality is to capture one or more digits surrounded by the given characters.

Matthew Flaschen
Got it, thank you for the explanation. Are there any good tutorial resources you'd recommend for learning RegEx??
kingrichard2005
[regular-expressions.info](http://www.regular-expressions.info/) is thorough.
Matthew Flaschen
Unclear from OP, but if you know the line begins with the `<...>` you can be extra safe by using `^<\[(\d+)\]>` to make sure the string is at the beginning of the line and not in the middle as a quote of a previous line or some such.
Peter Ajtai
A: 

Use:

/<\[([[:digit:]]+)\]>/

If your implementation doesn't support the handy [:digit:] syntax, then use this:

/<\[([\d]+)\]>/

And if your implementation doesn't support the handy \d syntax, then use this:

/<\[([0-9]+)\]>/
sarnold
Your last two are unbalanced.
Matthew Flaschen
@Matthew Flaschen, thanks! Perl didn't complain, and I didn't notice. Oops.
sarnold
A: 
$subject = "<[1085674730]> hello foo1, how are you doing?";
preg_match('/<\[(\d+)\]>/', $subject, $matches);

$matches[1] will contain the number you are looking for.

kiamlaluno
A: 

Hi kingrichard2005,

Combine Mathews post with lookarounds http://www.regular-expressions.info/lookaround.html. This will exclude the prefix and suffix.

(?<=<\[)\d+(?=\]>)

I didn't test this regex but it should be very close to what you need. Double check at the link provided.

Hope this helps!

Skyler