^[.-a-z0-9A-Z] what is the meaning of the given regular expression. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

^[.-a-z0-9A-Z] what is the meaning of the given regular expression.

1st May 2020, 10:11 AM
shreya arora
shreya arora - avatar
6 Answers
+ 1
it's not a valid regular expression... square braces are use to enclose class character. inside such classe, the dash (-) has a special meaning until it is placed at very first or last place... in any other cases, it define a character range ([a-z] match any character in the range of 'a' to 'z' both included) ^ at start of a regex match the string start, while at start inside a character class match any char not in the defined class (negate operator) Also, some special meaning char such as dot (.) outside a char class (matching any char) loose its special meaning inside a char classe (and match only the dot char itself)
1st May 2020, 10:27 AM
visph
visph - avatar
+ 1
~ swim ~ yes, we could modify it to make it valid (that's why my first post explain how are used char classes and other related things to OP code sample ;) Without the dot ([-a-z]) is valid and define all chars in a to z range AND the dash (-) wich at first or last place isn't interpreted as an operator defining a range... But in the case of the unmodified regex provided by OP, that cannot be determinated how to interpret (range between dot char and a plus dash char and z or dot and dash plus range between a and z? many more possibilities with the complete regex, but [.-a-z] seems enough to prove it ^^) Also negate char class, as I know, could be only done for the whole class (expect by using special char classes inside, such as \W or \S) through the use of the ^ special meaning operator at start of a char class (everywhere else inside a char clasd it would match the ^ char ;))
1st May 2020, 11:23 AM
visph
visph - avatar
+ 1
~ swim ~ :D my bad ^^ I was quickly typing the regex in a js script to confirm my knowledge based theory before my first post, and didn't read the error description given by either js linter and by running the code... but I was forgetting to close my regex literal with an ending slash (/)... so the error was unrelated to the regex itself but to my mistake ;P You're right, seems to be valid, and to be disambiguated as a char class for (in order) any of dot, dash, a to z, 0 to 9, and A to Z chars :) (probably a not well known and not well documented specification ^^)
1st May 2020, 11:51 AM
visph
visph - avatar
0
Eliya Ben Baruch and Lean Mijares : look carefully... how would you interprete the range [.-a-z] ? As I said, this regex is invalid ;P
1st May 2020, 10:32 AM
visph
visph - avatar
- 1
I talk about what I know from Java. It's like making a list of all the values from a to z (lowercase), 0 to 9, A to Z (uppercase). And in Java it's being used to replace these values, but in this case you got '^' which means to *not replace* them but all the rest although the '^' supposed to be inside the brackets (at least in Java it is).
1st May 2020, 10:24 AM
Eliya Ben Baruch
Eliya Ben Baruch - avatar
- 1
^ is used to tell the regular expression engine(regex) that the match must start at the begining of the text. so instead we type each character of the alphabet, we specify -a-z A-Z so that we'll both have uppercase and lowercase range. Same with number, we can simply have a range 0-9 instead of writing all 10 digits.
1st May 2020, 10:29 AM
tres