java中re正则表达式的一个疑惑
大家讲道理
大家讲道理 2017-04-18 09:31:00
0
2
403

java中,Pattern.matches("\\d+","2223");返回的是true。

如果Pattern.matches("\d+",匹配的又是什么呢?
比如换行符"\n"要匹配的时候,难道也要像上面那样写成"\\n"才能匹配成换行符?那re中"\n"匹配的又是什么呢?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
阿神

dYou will know after you try it. Error:
error: illegal escape character


+: Indicates a match greater than once
d+ matches d+匹配 d,...,ddd,...,ddd,... and the like


I didn’t pay attention to this issue before, and then I looked for some information. I also had problems with my original understanding. The answer just now was incomplete

It should be \nnBoth are fine

The first one is to directly turn it into n in the regex and be processed by the regex engine n被正则引擎处理
第二种是n被Java直接转化为一个newline character就是'u000A'The second one is to directly convert n into a newline character by Java, which is 'u000A 'Of course regular expressions can also be matched


The last question is to match the literal "\"+"n""\"+"n"
可以在编译正则的时候用Pattern.LITERAL 那样正则引擎就会把n当成nYou can use Pattern.LITERAL when compiling the regular expression. The engine will treat n as and n instead of matching newline characters. Of course, you should use the first method.

Pattern.compile("\n", Pattern.LITERAL );
左手右手慢动作

Because your language does not have a regular type, it can only be expressed as a string, so you need to escape it one more time.
\n 是正则的 n 匹配换行,n It’s a string change. Of course it’s correct to directly match line breaks

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template