Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of pound (#) in a regular expression

Tags:

regex

I have a regular expression with a pound # modifier.

What does \# mean in a regular expression? What does the # mean in a regular expression? Is it possible that Java is just ignoring these invalid modifiers?

like image 609
Ralph Oreg Avatar asked Aug 31 '12 18:08

Ralph Oreg


People also ask

What is pound example?

In simpler words, pounds tell us how heavy an object is. For example, the weight of a soccer ball is about one pound. A pound is expressed as lb or lbs, where “lb” stands for libra. It is a Latin word that means “balance” or “scale”.

What does pound mean in texting?

LBS is a textspeak acronym standing for laughing but serious.


2 Answers

\# is not a valid regex modifier. It would technically be a way to escape a # character, IF the # meant anything significant in Java's regex. Because it doesn't, Java is just ignoring this invalidity and reading it as a # instead of throwing you an error.

like image 104
Cat Avatar answered Sep 24 '22 23:09

Cat


I just had this same issue. It turned out that the prior programmer "missed" when changing all the "old code" from # to | and it was a simple mistake. I found this by checking all other code in the app and found NO other # symbols, but many similar areas of code with | included. And the | was right next to the # in this code. So someone was swapping out old regex for new regex and "missed".

like image 21
Bill Avatar answered Sep 23 '22 23:09

Bill