Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are these two regexes functionally equivalent?

Tags:

regex

Are the following two regular expressions functionally equivalent? I ask because I get different results when I interchange them so I guess I know the answer is that they are not equivalent. However, I don't understand why.

(,|$)

and

[,$]

I am basically looking for a comma or the end-of-line.

like image 578
GregH Avatar asked Dec 22 '22 02:12

GregH


2 Answers

The first one means a comma or end_of_line

The second means a comma or a dollar sign

Generaly special characters inside a class lose their special meaning.

So, for your question:

I am basically looking for a comma or the end-of-line.

Use the first one (,|$)

like image 141
Toto Avatar answered Dec 24 '22 00:12

Toto


I think that when you put $ into bracket it is considered like the dollar char and not the end of line

you will found more information here : http://www.regular-expressions.info/charclass.html

extract :

Metacharacters Inside Character Classes

Note that the only special characters or metacharacters inside a character class are the closing bracket (]), the backslash (), the caret (^) and the hyphen (-). The usual metacharacters are normal characters inside a character class, and do not need to be escaped by a backslash. To search for a star or plus, use [+*]. Your regex will work fine if you escape the regular metacharacters inside a character class, but doing so significantly reduces readability.

like image 30
Jerome Cance Avatar answered Dec 24 '22 02:12

Jerome Cance