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

3
  • just add - at last n character class, i.e change this [\w-_.] to [\w_.-] this Commented Jul 16, 2019 at 7:47
  • @CodeManiac what is the difference between these two? Commented Jul 16, 2019 at 7:48
  • 2
    - in between character class stand for range Commented Jul 16, 2019 at 7:49

2 Answers 2

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_.-]
Sign up to request clarification or add additional context in comments.

Comments

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).

7 Comments

This is not true in all the cases that - will be treated as range Related question
@CodeManiac I don't get your comment. Are you sure you've read my answer correctly ? Is it just about the formulation ?
@DenysSéguret what i mean is - in between character is always do not denote range, just read the question link i posted in comment
@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
This question isn't about JS; it's about Perl. There many regex languages, just like there are many programming languages.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.