-1

I am new to regex. What does regex expression match pattern "\[.*\]" mean?

If I have a text like "Hello [Here]", then success is returned in the match. And match contain [Here].

I read that:

. indicates Any except \n (newline),
* indicates 0 or more times

I don't understand the "\". It believe it is just escape sequence for "\".

So, is the expression "\[.*\]" trying to match a pattern like \[Any text\]?

halfer
  • 19,824
  • 17
  • 99
  • 186
Veeru
  • 19
  • 4
  • There is no `\.` in your pattern. It's `\[` to match `[` literal as `[` in regex is character class, it need to escaped to match opening square bracket. – Tushar Dec 28 '16 at 06:48
  • _is the expression "\[.*\]" trying to match a pattern like [Any text] ?_ Yes. It'll match text wrapped inside square brackets. But fail when there is nested brackets. – Tushar Dec 28 '16 at 06:49

2 Answers2

0

Yes, you are right. It will match any characters enclosed in []. The .* imply any or no characters enclosed in []. Also you should try this link which is a very helpful regex tool. You can input the regex pattern and check for matches easily.

Hima Varsha
  • 261
  • 1
  • 13
0

I have tried this on regexr, here is a screen shot:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Lucifer
  • 1,594
  • 2
  • 18
  • 32