[英]What does this regex syntax actually mean in Java?
I wrote a program to detect palindromes.我写了一个程序来检测回文。 It works with what I have, but I stumbled upon another bit of syntax, and I would like to know what it means exactly?
它适用于我所拥有的,但我偶然发现了另一种语法,我想知道它到底是什么意思?
This is the line of code I'm using:这是我正在使用的代码行:
userString = userString.toLowerCase().replaceAll("[^a-zA-Z]", "");
I understand that the replaceAll code snippet means to "match characters ([...]) that are not (^) in the range az and AZ (a-zA-Z)."我知道 replaceAll 代码片段的意思是“匹配 az 和 AZ (a-zA-Z) 范围内不 (^) 的字符 ([...])”。
However, this worked as well:但是,这也有效:
replaceAll("[^(\p{L}')]", "");
I just don't understand how to translate that into English.我只是不明白如何将其翻译成英文。 I am completely new to regular expressions, and I find them quite fascinating.
我对正则表达式完全陌生,我觉得它们很吸引人。 Thanks to anyone who can tell me what it means.
感谢任何能告诉我这意味着什么的人。
You should check this website: https://regex101.com你应该检查这个网站: https : //regex101.com
It helped me a lot when I was writing/testing/debugging some regexes ;)当我编写/测试/调试一些正则表达式时,它对我帮助很大;)
It gives the following explanation:它给出了以下解释:
[^(\\p{L}')]
match a single character not present in the list below: [^(\\p{L}')]
匹配以下列表中不存在的单个字符:
(
the literal character (
(
文字字符(
\\p{L}
matches any kind of letter from any language \\p{L}
匹配来自任何语言的任何类型的字母')
a single character in the list ')
literally ')
列表中的单个字符')
字面意思The two regexes are not the same:这两个正则表达式不一样:
[^a-zA-Z]
matches any char not an English letter [^a-zA-Z]
匹配任何不是英文字母的字符[^(\\p{L}')]
matches any char not a letter, quote or bracket [^(\\p{L}')]
匹配任何字符而不是字母、引号或括号ie the 2nd one removes brackets and quotes too.即第二个也删除了括号和引号。
The regex \\p{L}
is the posix character class for "any letter".正则表达式
\\p{L}
是“任何字母”的 posix 字符类。 IE these two regexes are equivalent in the context of letters only from English: IE 这两个正则表达式仅在英文字母的上下文中是等效的:
[a-zA-Z]
\\p{L}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.