开发者

I'm trying to match some numbers in a string using a regexpressions and am having difficulty with the syntax

here is the line i'm trying to parse

[\\?\Volume{d开发者_StackOverflow3f7f470-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


.*\] (\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


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.)


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

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

It will match exactly twice: 85 and 90


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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜