1

I have a text like this;

[Some Text][1][Some Text][2][Some Text][3][Some Text][4]

I want to match [Some Text][2] with this regex;

/\[.*?\]\[2\]/

But it returns [Some Text][1][Some Text][2]

How can i match only [Some Text][2]?

Note : There can be any character in Some Text including [ and ] And the numbers in square brackets can be any number not only 1 and 2. The Some Text that i want to match can be at the beginning of the line and there can be multiple Some Texts

JSFiddle

MOD
  • 1,010
  • 3
  • 18
  • 39

2 Answers2

1

The \[.*?\]\[2\] pattern works like this:

  • \[ - finds the leftmost [ (as the regex engine processes the string input from left to right)
  • .*? - matches any 0+ chars other than line break chars, as few as possible, but as many as needed for a successful match, as there are subsequent patterns, see below
  • \]\[2\] - ][2] substring.

So, the .*? gets expanded upon each failure until it finds the leftmost ][2]. Note the lazy quantifiers do not guarantee the "shortest" matches.

Solution

Instead of a .*? (or .*) use negated character classes that match any char but the boundary char.

\[[^\]\[]*\]\[2\]

See this regex demo.

Here, .*? is replaced with [^\]\[]* - 0 or more chars other than ] and [.

Other examples:

  • <[^<>]*> matches <...> with no < and > inside
  • \([^()]*\) matches (...) with no ( and ) inside
  • "[^"]*" matches "..." with no " inside

In other situations, when the starting pattern is a multichar string or complex pattern, use a tempered greedy token, (?:(?!start).)*?. To match abc 1 def in abc 0 abc 1 def, use abc(?:(?!abc).)*?def.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
0

You could try the below regex,

(?!^)(\[[A-Z].*?\]\[\d+\])    

DEMO

Avinash Raj
  • 160,498
  • 22
  • 182
  • 229
  • It does not return the value i want if there is a `[` or `]` in `Some Text` – MOD Aug 30 '14 at 12:23
  • could you provide all the possibilities? – Avinash Raj Aug 30 '14 at 12:24
  • I am sorry @Avinash Raj But your answers make me remember other possibilities. The `Some Text` that i want to match can be at the beginning of the line and there can be multiple `Some Text`s – MOD Aug 30 '14 at 12:53
  • that's what i said before. Please post some examples for all the possible matches and also for not. – Avinash Raj Aug 30 '14 at 12:54
  • please not in the literal form it's hard to understand. – Avinash Raj Aug 30 '14 at 12:55
  • see here http://regex101.com/r/qI4aA6/6 it matches any number the corresponding `[]` if there are any number of sometext or [] present in it. THe main part in the above regex is `\[[A-Z]` – Avinash Raj Aug 30 '14 at 12:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60293/discussion-between-mod-and-avinash-raj). – MOD Aug 30 '14 at 13:02