Java RegEx 元字符 (.) 和普通点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3674930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java RegEx meta character (.) and ordinary dot?
提问by JavaUser
In Java RegEx, how to find out the difference between .
(dot) the meta character and the normal dot as we using in any sentence. How to handle this kind of situation for other meta characters too like (*
,+
,/d
,...)
在Java RegEx中,如何找出.
(点)元字符和我们在任何句子中使用的普通点之间的区别。如何处理其他元字符的这种情况,例如 ( *
, +
, /d
,...)
采纳答案by Fabian Steeg
If you want the dot or other characters with a special meaning in regexes to be a normal character, you have to escape it with a backslash. Since regexes in Java are normal Java strings, you need to escape the backslash itself, so you need two backslashes e.g. \\.
如果您希望正则表达式中具有特殊含义的点或其他字符成为普通字符,则必须使用反斜杠对其进行转义。由于Java中的正则表达式是普通的Java字符串,因此您需要对反斜杠本身进行转义,因此您需要两个反斜杠,例如\\.
回答by Christoffer Hammarstr?m
Escape special characters with a backslash. \.
, \*
, \+
, \\d
, and so on. If you are unsure, you may escape any non-alphabetical character whether it is special or not. See the javadoc for java.util.regex.Patternfor further information.
使用反斜杠转义特殊字符。\.
, \*
, \+
, \\d
, 等等。如果您不确定,您可以转义任何非字母字符,无论它是否特殊。有关更多信息,请参阅java.util.regex.Pattern 的 javadoc。
回答by Tim Pietzcker
Perl-style regular expressions (which the Java regex engine is more or less based upon) treat the following characters as special characters:
Perl 样式的正则表达式(Java 正则表达式引擎或多或少基于它)将以下字符视为特殊字符:
.^$|*+?()[{\
have special meaning outsideof character classes,
.^$|*+?()[{\
在字符类之外具有特殊含义,
]^-\
have special meaning insideof character classes ([...]
).
]^-\
在字符类 ( [...]
) 中具有特殊含义。
So you need to escape those (and only those) symbols depending on context (or, in the case of character classes, place them in positions where they can't be misinterpreted).
因此,您需要根据上下文对那些(并且仅是那些)符号进行转义(或者,在字符类的情况下,将它们放在不会被误解的位置)。
Needlessly escaping other characters may work, but some regex engines will treat this as syntax errors, for example \_
will cause an error in .NET.
不必要地转义其他字符可能会起作用,但一些正则表达式引擎会将其视为语法错误,例如\_
会导致 .NET 中的错误。
Some others will lead to false results, for example \<
is interpreted as a literal <
in Perl, but in egrep
it means "word boundary".
其他一些会导致错误的结果,例如在 Perl 中\<
被解释为文字<
,但在egrep
它的意思是“词边界”。
So write -?\d+\.\d+\$
to match 1.50$
, -2.00$
etc. and [(){}[\]]
for a character class that matches all kinds of brackets/braces/parentheses.
所以写-?\d+\.\d+\$
来的比赛1.50$
,-2.00$
等,并[(){}[\]]
为一个字符类匹配各种支架/支架/括号。
If you need to transform a user input string into a regex-safe form, use java.util.regex.Pattern.quote
.
如果您需要将用户输入字符串转换为正则表达式安全形式,请使用java.util.regex.Pattern.quote
.
Further reading: Jan Goyvaert's blog RegexGuru on escaping metacharacters
回答by Atspulgs
I wanted to match a string that ends with ".*" For this I had to use the following:
我想匹配以“.*”结尾的字符串为此我必须使用以下内容:
"^.*\.\*$"
Kinda silly if you think about it :D Heres what it means. At the start of the string there can be any character zero or more times followed by a dot "." followed by a star (*) at the end of the string.
如果你仔细想想,这有点傻:D 这就是它的意思。在字符串的开头可以有零次或多次后跟一个点“.”的任何字符。后跟字符串末尾的星号 (*)。
I hope this comes in handy for someone. Thanks for the backslash thing to Fabian.
我希望这对某人有用。感谢对 Fabian 的反斜杠。
回答by Kael
Solutions proposed by the other members don't work for me.
其他成员提出的解决方案对我不起作用。
But I found this :
但我发现了这一点:
to escape a dot in java regexp write [.]
在 java regexp write 中转义一个点 [.]
回答by Vyankatesh S Repal
Here is code you can directly copy paste :
这是您可以直接复制粘贴的代码:
String imageName = "picture1.jpg";
String [] imageNameArray = imageName.split("\.");
for(int i =0; i< imageNameArray.length ; i++)
{
system.out.println(imageNameArray[i]);
}
And what if mistakenly there are spaces left before or after "." in such cases? It's always best practice to consider those spaces also.
如果错误地在“。”之前或之后留下了空格怎么办?在这种情况下?同时考虑这些空间始终是最佳实践。
String imageName = "picture1 . jpg";
String [] imageNameArray = imageName.split("\s*.\s*");
for(int i =0; i< imageNameArray.length ; i++)
{
system.out.println(imageNameArray[i]);
}
Here, \\s* is there to consider the spaces and give you only required splitted strings.
在这里, \\s* 用于考虑空格并只为您提供所需的拆分字符串。
回答by Madhu Gogineni
If you want to end check whether your sentence ends with "." then you have to add [\.\]$ to the end of your pattern.
如果你想结束检查你的句子是否以“. ”结尾,那么你必须在模式的末尾添加 [\.\]$ 。
回答by rgm
I am doing some basic array in JGrasp and found that with an accessor method for a char[][] array to use ('.') to place a single dot.
我在 JGrasp 中做了一些基本的数组,发现使用 char[][] 数组的访问器方法使用 ('.') 放置一个点。