tags:

views:

52

answers:

5

here is the line i'm trying to parse

[\\?\Volume{d3f7f470-526b-11df-92eb-001a647802d2}\] 85 90 NotFound

I'm basically just trying to get the numbers that are outside of the brackets and ignore anything in between the brackets.

My original syntax worked until I realized that sometimes there would be numbers in the brackets (I was just using "([0-99]{2})")

any help would be greatly appreciated.

Thanks

A: 

/\b(\d{2})\b(\d{2})\b/

Then you can pull out the numbers in whichever environment you're using the regex.

mmsmatt
This won't match anything. `\b` is a zero-width assertion, so you're expecting a pair of two-digit numbers with a word-boundary, and nothing else, in between them, which is impossible.
Marcelo Cantos
@Marcelo: Ah, thanks for clarifying.
mmsmatt
/\b(\d{2})\b\s+\b(\d{2})\b/ would work; or even /(\d{2})\s(\d{2})/ would work for this case.
Senseful
+2  A: 

Assuming Perl compatible regexes:

`(?<=[\]\d] )(\d{2})(?= )`

This matches two digits that are preceded by a ] or a digit then a space and followed by a space. It also doesn't gobble up the space, so you can match both strings in, say, a global substitution operation. If you want to match all the numbers (and assuming there are only two, by definition):

`\] (\d{2}) (\d{2}) `

(The look-around assertions are no longer required in this case.)

Marcelo Cantos
+1, that's a good solution.
Tomalak
i looked at doing this earlier, but there could be a case where the line would have numbers in between the brackets with spaces where i didn't want it to match.
Ryan
@Ryan, it is difficult to fully address the requirements without understanding the full breadth of possible inputs. I've added some extra checks to cover this particular concern. Do you want to match both numbers at once, or each number individually?
Marcelo Cantos
@Marcelo Cantos the way the script works currently it would be helpful if it matched each number individually. The script is written in VBscript too.
Ryan
+1  A: 

Use a positive look-behind construct (to look for a ]):

(?<=].*)(\d{2})

It will match exactly twice: 85 and 90

tanascius
Look-around assertions have to be fixed length (in Perl, at least). `.*` is illegal. Also, there's no need to match `9` twice.
Marcelo Cantos
@Marcelo: well, I don't know perl syntax - this is .net and it works as given. You are right about matching 9 twice - I just reused the original regex ... @Ryan: which language do you use?
tanascius
@tanascius: Variable-length look-behinds do not work in many regex flavors - .NET is an exception, this is something to keep in mind.
Tomalak
@Marcelo this will be in vbscript.
Ryan
+3  A: 
.*\] (\d+) (\d+)
  1. .* will go right to the end of the string
  2. \] will backtrack to the closing square bracket
  3. (\d+) will match the first number (use \d{2} if you want 2 digit numbers only)
  4. (\d+) will match the second number
Tomalak
A: 

Because vbscript is so picky about regexes what I ended up doing was matching everything between the brackets \[(.*)\] doing a replace on the string to get rid of them altogether and then used the regex \d+ to grab the remaining numbers.

Ryan