[英]Matching String regular expression in java?
I have below lines of java code. 我有下面的java代码行。
String string1 = "SAMPLE STRING";
String string2 = "SAMPLE*";
string2 = string2.replaceAll("\\*",".*").trim().toUpperCase();
if(string1.matches(string2)){
System.out.println("true");
}else{
System.out.println("false");
}
Here i dont understand the regex meaning here. 在这里,我不明白这里的正则表达式意义。 what does it mean?
这是什么意思? Can anyone please help me?
谁能帮帮我吗?
Thanks! 谢谢!
So your regex here is "\\\\*"
which simply means character *
. 所以你的正则表达式是
"\\\\*"
,这只是意味着字符*
。 The replaceAll()
here will replace all *
characters in string2
with .*
. 这里的
replaceAll()
将用.*
替换string2
所有*
字符。
This is a common replace to have people use simplified regexes (like the ones used to match files, eg *.exe
) and convert it to a PCRE regex , which is the regex used in Java. 这是人们使用简化正则表达式(如用于匹配文件的正则表达式 ,例如
*.exe
)并将其转换为PCRE正则表达式 (这是Java中使用的正则表达式)的常见替代。
Regex "\\*" simply means ' ', when applied on string2 = SAMPLE by replaceAll() the string2 became SAMPLE.* and this is again a regex used by Matcher for match(). 正则表达式“\\ *”仅表示' ',当通过replaceAll() 应用于string2 = SAMPLE时 ,string2变为SAMPLE。*并且这又是匹配器()匹配器使用的正则表达式。
"SAMPLE.*" simply accepts everything followed by "SAMPLE" “SAMPLE。*”只接受一切后跟“SAMPLE”
Hence, "SAMPLE STRING" matched "SAMPLE.*". 因此,“SAMPLE STRING”匹配“SAMPLE。*”。
Similarly, "SAMPLE ", "SAMPLE 123SSS", "SAMPLE" etc. will match. 类似地,“SAMPLE”,“SAMPLE 123SSS”,“SAMPLE”等将匹配。 But, " SAMPLE STRING" doesn't matches pattern "SAMPLE.*"
但是,“SAMPLE STRING”与模式“SAMPLE。*”不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.