-2

I need to match foo1 in following text [@foo1].

So Im using this regex @([A-z0-9]*).

The problem is, it also matches the ending square bracket so I get foo1] as result.

regex101.com example

So the questions are:

  • Why is the ending square bracket included in the result?
  • How do I write regex to match only foo1
Buksy
  • 11,571
  • 9
  • 62
  • 69
  • I don't see a reason to downvote if the answer wasn't obvious. I wasn't able to search for any similar post. Actually, the answers may be duplicate, but the question is not. – Buksy Feb 09 '20 at 21:20

2 Answers2

0

Because A-Za-z0-9 was required..

Thinking about ASCII or whatever encoding is used. I bet ] lies within A-z!

See here using the above regex

Yup.. 93 according to this

If you allowed _, too - you would have been able to use \w*

JGFMK
  • 8,425
  • 4
  • 58
  • 92
0

You've specified A-z. Let's look at an ASCII chart:

enter image description here

A is 41 hex, and z is 7A hex, and in between them at 5D hex is ]. That's why the ] is included in the match - because that's what you asked it to do.

You might want to try @([A-Za-z0-9]+) instead.