Meaning of pound (#) in a regular expression

前端 未结 3 1050
野的像风
野的像风 2021-02-19 05:59

I have a regular expression with a pound # modifier.

What does \\# mean in a regular expression? What does the # mean in a regul

相关标签:
3条回答
  • 2021-02-19 06:09

    As you have suspected, it's just a mistake.

    To be sure, I ran:

    String quote = Pattern.quote("_#_");
    

    and the output was:

    "\Q_#_\E"
    

    So, clearly, the pound symbol ("#") is not a special character for java regex.

    See: Pattern.quote()

    0 讨论(0)
  • 2021-02-19 06:26

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

    0 讨论(0)
  • 2021-02-19 06:27

    \# 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.

    0 讨论(0)
提交回复
热议问题