0

I'm using a regex to check the name of the files. Now the console from Intellij shows me this message:

False [] range "\w-" in regex; marked by <-- HERE in m/^deploy-file-\d+_[\w <-- HERE _.]+\d+.log$/ at ...

Code

elsif ($filename =~ m/^deploy-file-\d+_[\w-_.]+\d+\.log$/) {
   print "$filename match with pattern\n";
}

I wanted to say that everything is working and the pattern works fine so far. My question is, why does intellij show me this message? What is the problem? And does it have any effects on the program?

My pattern also works on https://regex101.com/r/Lj2r4r/2

poisn
  • 437
  • 2
  • 17

2 Answers2

4

- is special in bracketed character classes ([...]). It can be used to specify ranges of characters.

[a-z]

However, the range you specified ([\w-_]) doesn't make any sense. You presumably meant to match _ literally, which can be done by escaping the -.

[\w\-_.]

Placing the - at the start or end of the character class also works.

[\w_.-]
ikegami
  • 367,544
  • 15
  • 269
  • 518
3

A dash in a character set (between [ and ]) is usually used to define a range. You should either move the dash to the end of the set definition or, better, escape that dash:

[\w\-_.]

(note that not all regex engines accept an unescaped dash just because it's at the end, so I don't recommend it in general).

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • This is not true in all the cases that `-` will be treated as range [`Related question`](https://stackoverflow.com/questions/56572373/how-word-character-is-interpreted-in-character-class) – Code Maniac Jul 16 '19 at 07:51
  • @CodeManiac I don't get your comment. Are you sure you've read my answer correctly ? Is it just about the formulation ? – Denys Séguret Jul 16 '19 at 07:52
  • @DenysSéguret what i mean is `-` in between character is always do not denote range, just read the question link i posted in comment – Code Maniac Jul 16 '19 at 07:54
  • 1
    @CodeManiac if you read my answer you'll see I'm well aware of this. And don't use that trick without knowing your engine well enough: it depends on the engine *and version* – Denys Séguret Jul 16 '19 at 07:54
  • @Code Maniac, Re "*what i mean is - in between character is always do not denote range*", huh? It does in character classes, unless it's at the end, at the start, after a leading `^`, at the start/end of a range, or if it's escaped. Is that what you meant? – ikegami Jul 16 '19 at 07:57
  • @ikegami no it doesn't the question link i posted is regarding that only, in `JS` if use `[\w-~]` `-` is not treated as range, where as in `[A-Za-z0-9_-~]` `-` is treated as range – Code Maniac Jul 16 '19 at 08:01
  • 1
    This question isn't about JS; it's about Perl. There many regex languages, just like there are many programming languages. – ikegami Jul 16 '19 at 08:01