java 只匹配字符串的第一个和最后一个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6620758/
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
Match only first and last character of a string
提问by Sinker
I had a look at other stackoverflowquestions and couldn't find one that asked the same question, so here it is:
我查看了其他stackoverflow问题,但找不到提出相同问题的问题,所以这里是:
How do you match the first and last characters of a string (can be multi-line or empty).
如何匹配字符串的第一个和最后一个字符(可以是多行或空)。
So for example:
例如:
String = "this is a simple sentence"
Note that the string includes the beginning and ending quotation marks.
请注意,该字符串包括开头和结尾的引号。
How do I get match the first and last characters where the string begins and ends with a quotation mark (").
如何匹配字符串以引号 (") 开头和结尾的第一个和最后一个字符。
I tried:
我试过:
^"|$" and \A"\Z"
but these do not produce the desired result.
但这些不会产生预期的结果。
Thanks for your help in advance :)
提前感谢您的帮助:)
采纳答案by wjans
Is this what you are looking for?
这是你想要的?
String input = "\"this is a simple sentence\"";
String result = input.replaceFirst("(?s)^\"(.*)\"$", " ");
This will replace the first and last character of the input string with spaces if it starts and ends with "
. It will also work across multiple lines since the DOTALL
flag is specified by (?s)
.
如果输入字符串的开头和结尾以"
. 由于DOTALL
标志由(?s)
.
回答by Bohemian
The regex that matches the whole input ".*"
. In java, it looks like this:
匹配整个 input 的正则表达式".*"
。在java中,它看起来像这样:
String regex = "\".*\"";
System.out.println("\"this is a simple sentence\"".matches(regex)); // true
System.out.println("this is a simple sentence".matches(regex)); // false
System.out.println("this is a simple sentence\"".matches(regex)); // false
If you want to removethe quotes, use this:
如果要删除引号,请使用以下命令:
String input = "\"this is a simple sentence\"";
input = input.replaceAll("(^\"|\"$)", "")); // this is a simple sentence (without any quotes)
If you want this to work over multiple lines, use this:
如果您希望它在多行上工作,请使用:
String input = "\"this is a simple sentence\"\n\"and another sentence\"";
System.out.println(input + "\n");
input = input.replaceAll("(?m)(^\"|\"$)", "");
System.out.println(input);
which produces output:
产生输出:
"this is a simple sentence"
"and another sentence"
this is a simple sentence
and another sentence
Explanation of regex (?m)(^"|"$)
:
正则表达式的解释(?m)(^"|"$)
:
(?m)
means "Caret and dollar match after and before newlines for the remainder of the regular expression"(^"|"$)
means^"
OR"$
, which means "start of line then a double quote" OR "double quote then end of line"
(?m)
表示“正则表达式其余部分的换行符前后的插入符和美元匹配”(^"|"$)
意味着^"
OR"$
,这意味着“行首然后双引号”或“双引号然后行尾”
回答by Kaj
String regexp = "(?s)\".*\"";
String data = "\"This is some\n\ndata\"";
Matcher m = Pattern.compile(regexp).matcher(data);
if (m.find()) {
System.out.println("Match starts at " + m.start() + " and ends at " + m.end());
}
回答by Sanjay T. Sharma
Why not use the simple logic of getting the first and last characters based on charAt
method of String? Place a few checks for empty/incomplete strings and you should be done.
为什么不使用基于charAt
String的方法获取第一个和最后一个字符的简单逻辑?对空/不完整的字符串进行一些检查,您应该完成。