正则表达式 Java 字符串按单个星号拆分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15492353/
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
Regex Java String Split by Single Asterisk
提问by princepiero
Help is needed.
需要帮助。
line.split("*");
I used this line of code to split a string into an asterisk mark. However, I got an error from my compiler. It says, "INVALID REGULAR EXPRESSION: DANGLING META CHARACTER '*'"
我使用这行代码将字符串拆分为星号标记。但是,我的编译器出错了。它说,“无效的正则表达式:悬空的元字符'*'”
How to resolve this problem? Thanks in advance.
如何解决这个问题?提前致谢。
回答by squiguy
*
has special meaning in regular expressions. You have to escape it.
*
在正则表达式中具有特殊意义。你必须逃避它。
line.split("\*");
回答by Aziz Shaikh
Try this statement:
试试这个语句:
line.split("\*");
回答by Cris_Towi
It is because you used a "*", that is a regular expression. If you want to use this caracter, you need tu put something like that:
这是因为您使用了“*”,即正则表达式。如果你想使用这个角色,你需要输入这样的东西:
line.split("\*");
回答by Ankur Shanbhag
*is a meta character in regular expression. It is used for matching 0 or more elements. If you want to use *as a normal character and not as a special character (i.e. skip its behavior as a meta character) then add escape characters before it.
*是正则表达式中的元字符。它用于匹配 0 个或多个元素。如果您想将*用作普通字符而不是特殊字符(即跳过其作为元字符的行为),则在它之前添加转义字符。
Eg: String[] split = line.split("\\*");
例如: String[] split = line.split("\\*");
Hope this helps.
希望这可以帮助。
回答by Manish Kumar
Use this
用这个
" String data= "Mani*Kum";
" String data="玛尼*库姆";
String []value= data.split("\*");
" The output will like this:
" 输出将是这样的:
value[0]= "Mani";
value[1]= "Kum";