Regex gets square brackets containing only numbers, but not in square brackets

Line example

"[] [ds*[000112]] [1448472995] sample string [1448472995] ***"; 

The regular expression must match

  [1448472995] [1448472995] 

and should not match [000112] , since there is an external square bracket.

I currently have this regex that also matches [000112]

 const string unixTimeStampPattern = @"\[([0-9]+)]"; 
+6
source share
4 answers

This is a good way to do this using balanced text.

  ( \[ \d+ \] ) # (1) | # or, \[ # Opening bracket (?> # Then either match (possessively): [^\[\]]+ # non - brackets | # or \[ # [ increase the bracket counter (?<Depth> ) | # or \] # ] decrease the bracket counter (?<-Depth> ) )* # Repeat as needed. (?(Depth) # Assert that the bracket counter is at zero (?!) ) \] # Closing bracket 

C # sample

 string sTestSample = "[] [ds*[000112]] [1448472995] sample string [1448472995] ***"; Regex RxBracket = new Regex(@"(\[\d+\])|\[(?>[^\[\]]+|\[(?<Depth>)|\](?<-Depth>))*(?(Depth)(?!))\]"); Match bracketMatch = RxBracket.Match(sTestSample); while (bracketMatch.Success) { if (bracketMatch.Groups[1].Success) Console.WriteLine("{0}", bracketMatch); bracketMatch = bracketMatch.NextMatch(); } 

Output

 [1448472995] [1448472995] 
+4
source

You need to use balancing groups to handle this - it looks a little more complicated, but not all the more complicated:

 Regex regexObj = new Regex( @"\[ # Match opening bracket. \d+ # Match a number. \] # Match closing bracket. (?= # Assert that the following can be matched ahead: (?> # The following group (made atomic to avoid backtracking): [^\[\]]+ # One or more characters except brackets | # or \[ (?<Depth>) # an opening bracket (increase bracket counter) | # or \] (?<-Depth>) # a closing bracket (decrease bracket counter, can't go below 0). )* # Repeat ad libitum. (?(Depth)(?!)) # Assert that the bracket counter is now zero. [^\[\]]* # Match any remaining non-bracket characters \z # until the end of the string. ) # End of lookahead.", RegexOptions.IgnorePatternWhitespace); 
+4
source

Are you just trying to grab a unix timestamp? Then you can try a simpler option, in which you specify the minimum number of characters to be matched in the group.

 \[([0-9]{10})\] 

Here I limit it to 10 characters, since I doubt that the timestamp will hit 11 characters in the near future ... To protect against this:

 \[([0-9]{10,11})\] 

Of course, this can lead to false positives if you have a 10-bit number in the enclosed bracket.

0
source

This will match your expression as expected: http://regexr.com/3csg3 uses lookahead.

-2
source

All Articles