-4

What is the meaning of this regular expression?

    ['`?!\"-/]

Why it matches parenthesis?

I used Java for development

Urgent
  • 1
  • 2

3 Answers3

2

In your regex

['`?!\"-/]

The quantity "-/ is being interpreted as a range of values, just as A-Z would mean taking every letter between A and Z. It turns out, by reading the basic ASCII table, that parentheses lie within this range, so your pattern is including them.

One trick you can use here with dash is to place it at the end:

['`?!\"/-]
        ^^^^ this will not be interpreted as a range
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
1

Because you didn't escape the dash -. The dash, inside a character class [] denotes a range of characters. In this case from " to /. And parentheses are between those, in ASCII.

The dash needs to be escaped \-, if it's not the first or last character, inside a character class, when you want it to be matched as a literal.

Decent Dabbler
  • 21,401
  • 8
  • 70
  • 101
0

You have to use following You need to escape -, otherwise, parentheses are matching. Seems like "-/ will include parentheses as well. Like [A-C], which matches ASCII chars between A to C

[\'`?!\"\-/]

It will match following characters in a string.

'`?"-/

Check in the regex101

dwij
  • 664
  • 6
  • 15