获取引号后字符串中的第一个单词 " (Java)
Getting the first word in a String after quotation " (Java)
我有以下字符串:
String sentence = "this is my sentence \"course of math\" of this year";
我需要得到像这样的引语后的第一个词 "
。
在我的例子中,我会得到这个词:course.
真的很简单,试试这个:
/"(\w+)/
并且您可以使用 </code></p> 来获得预期的单词
<ul>
<li><code>"
匹配字符 " literally
(
捕获组
\w+
匹配任意单词字符[a-zA-Z0-9_]
Online Demo
备选方案replaceAll
approach:
String sentence = "this is my sentence \"course of math\" of this year";
System.out.println(sentence.replaceAll("(?s)[^\"]*\"(\w+).*", ""));
// Or - if there can be a space after the first quote:
sentence = "this is my sentence \" course of math\" of this year";
System.out.println(sentence.replaceAll("(?s)[^\"]*\"\s*(\w+).*", ""));
它 returns course
因为模式抓取第一个 "
之前的任何字符(使用 [^"]*
),然后匹配引号,然后匹配并捕获 1 + 字母数字或下划线字符((\w+)
),然后匹配任何 0+ 字符直到结尾(.*
),我们将其全部替换为第 1 组的内容。
以防万一有人想知道是否也可以使用非正则表达式解决方案,这是一个不支持第一个 "
和单词之间的空格的解决方案:
String sentence = "this is my sentence \"course of math\" of this year";
String[] MyStrings = sentence.split(" "); // Split with a space
String res = "";
for(int i=0; i < MyStrings.length; i++) // Iterate over the split parts
{
if(MyStrings[i].startsWith("\"")) // Check if the split chunk starts with "
{
res = MyStrings[i].substring(1); // Get a substring from Index 1
break; // Stop the iteration, yield the value found first
}
}
System.out.println(res);
还有一个支持第一个 "
和下一个单词之间的空格:
String sentence = "this is my sentence \" course of math\" of this year";
String[] MyStrings = sentence.split("\"");
String res = MyStrings.length == 1 ? MyStrings[0] : // If no split took place use the whole string
MyStrings[1].trim().indexOf(" ") > -1 ? // If the second element has space
MyStrings[1].trim().substring(0, MyStrings[1].trim().indexOf(" ")): // Get substring
MyStrings[1]; // Else, fetch the whole second element
System.out.println(res);
我有以下字符串:
String sentence = "this is my sentence \"course of math\" of this year";
我需要得到像这样的引语后的第一个词 "
。
在我的例子中,我会得到这个词:course.
真的很简单,试试这个:
/"(\w+)/
并且您可以使用 </code></p> 来获得预期的单词
<ul>
<li><code>"
匹配字符 " literally
(
捕获组\w+
匹配任意单词字符[a-zA-Z0-9_]
Online Demo
备选方案replaceAll
approach:
String sentence = "this is my sentence \"course of math\" of this year";
System.out.println(sentence.replaceAll("(?s)[^\"]*\"(\w+).*", ""));
// Or - if there can be a space after the first quote:
sentence = "this is my sentence \" course of math\" of this year";
System.out.println(sentence.replaceAll("(?s)[^\"]*\"\s*(\w+).*", ""));
它 returns course
因为模式抓取第一个 "
之前的任何字符(使用 [^"]*
),然后匹配引号,然后匹配并捕获 1 + 字母数字或下划线字符((\w+)
),然后匹配任何 0+ 字符直到结尾(.*
),我们将其全部替换为第 1 组的内容。
以防万一有人想知道是否也可以使用非正则表达式解决方案,这是一个不支持第一个 "
和单词之间的空格的解决方案:
String sentence = "this is my sentence \"course of math\" of this year";
String[] MyStrings = sentence.split(" "); // Split with a space
String res = "";
for(int i=0; i < MyStrings.length; i++) // Iterate over the split parts
{
if(MyStrings[i].startsWith("\"")) // Check if the split chunk starts with "
{
res = MyStrings[i].substring(1); // Get a substring from Index 1
break; // Stop the iteration, yield the value found first
}
}
System.out.println(res);
还有一个支持第一个 "
和下一个单词之间的空格:
String sentence = "this is my sentence \" course of math\" of this year";
String[] MyStrings = sentence.split("\"");
String res = MyStrings.length == 1 ? MyStrings[0] : // If no split took place use the whole string
MyStrings[1].trim().indexOf(" ") > -1 ? // If the second element has space
MyStrings[1].trim().substring(0, MyStrings[1].trim().indexOf(" ")): // Get substring
MyStrings[1]; // Else, fetch the whole second element
System.out.println(res);