Reputation: 875
what does the following regex do?
[\w+\-.]+
Output of the above regex, \w+-.+ and \w+\-.+ is same, but I am not able to understand why?
Test string
sadkfj-dslk.sdjklf!sdljf
Can you explain this to me?
Upvotes: 0
Views: 106
Reputation: 2017
This matches a 'word character' [0-9a-z-A-Z_] (\w matches word character), OR a + character, OR a hyphen (\ escapes the -), OR a full stop, where all of this is repeated more than once.
This means it'll match any string of letters containing normal letters, underscores, plusses, hyphens or full stops, provided that there are no spaces. In short - this seems to me to be a function to identify a string from a URL to locate a database entry.
There is no difference because escaping the - isn't necessary, as there is no character match "+ to .", so escaped or unescaped, it makes no difference.
Upvotes: 1
Reputation: 477040
Inside the square brackets, the hyphen has special meaning ("range"), so a literal hyphen should be escaped as \-
. However, depending on your particular regex engine, you may get away with the unescaped syntax since in this particular case there's no ambiguity.
(For example, Perl will accept both versions and produce the expected result, but with use warnings;
it will complain about the missing backslash.)
Upvotes: 2
Reputation: 723628
It's the same because the backslash is superfluous; you don't actually need to escape the -
when it doesn't denote a valid character range (that is, +-.
is not a character range), as it'll be treated as a literal dash anyway regardless.
The given regex looks for one or more of any of these characters: \w
(a word character), +
(plus sign), -
(dash) or .
(period). As the backslash before -
is unnecessary, whether you include it or not makes no difference.
Upvotes: 3