Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何将某个短语拆分为字符串?_Regex_String_Split_Match - Fatal编程技术网

Regex 如何将某个短语拆分为字符串?

Regex 如何将某个短语拆分为字符串?,regex,string,split,match,Regex,String,Split,Match,我有一些搜索项目的字符串,我想把它们分割成一个字符串数组 例如: String text = "java example \"this is a test\" hello world"; 我想得到以下结果 result[0] = "java"; result[1] = "example"; result[2] = "\"this is a test\""; result[3] = "hello"; result[4] = "world"; 简言之,我想要组合text.split(“”)和te

我有一些搜索项目的字符串,我想把它们分割成一个字符串数组

例如:

String text = "java example \"this is a test\" hello world";
我想得到以下结果

result[0] = "java";
result[1] = "example";
result[2] = "\"this is a test\"";
result[3] = "hello";
result[4] = "world";
简言之,我想要组合text.split(“”)和text.split(\“”); 有没有简单的编码方法


谢谢!

这个正则表达式应该匹配
(\\“+?\\”)|([^\s]+)

它匹配
\“
中的任何内容,包括
\”
或单个单词


检查这里的结果:

这个正则表达式应该匹配
(\\“+?\”)\124;([^\ s]+)

它匹配
\“
中的任何内容,包括
\”
或单个单词


检查这里的结果:

您可以在
String#split
方法中使用此正则表达式:

(?=(([^\"]*\"){2})*[^\"]*$)\\s+
代码:

String text = "java example \"this is a test\" hello world";
String[] tok = text.split("(?=(([^\"]*\"){2})*[^\"]*$)\\s+");
// print the array
System.out.println( Arrays.toString( arr ) );
[java, example, "this is a test", hello, world]
输出:

String text = "java example \"this is a test\" hello world";
String[] tok = text.split("(?=(([^\"]*\"){2})*[^\"]*$)\\s+");
// print the array
System.out.println( Arrays.toString( arr ) );
[java, example, "this is a test", hello, world]

您可以在
String#split
方法中使用此正则表达式:

(?=(([^\"]*\"){2})*[^\"]*$)\\s+
代码:

String text = "java example \"this is a test\" hello world";
String[] tok = text.split("(?=(([^\"]*\"){2})*[^\"]*$)\\s+");
// print the array
System.out.println( Arrays.toString( arr ) );
[java, example, "this is a test", hello, world]
输出:

String text = "java example \"this is a test\" hello world";
String[] tok = text.split("(?=(([^\"]*\"){2})*[^\"]*$)\\s+");
// print the array
System.out.println( Arrays.toString( arr ) );
[java, example, "this is a test", hello, world]

我认为您有点困惑,代码中有错误! 组成字符串应为:

String text = "java example \"this is a test\" hello world";
变量
text
的值将是:

java example "this is a test" hello world
我假设您希望将其提取到以下数组中:

result[0] = "java";
result[1] = "example";
result[2] = "\"this is a test\"";
result[3] = "hello";
result[4] = "world";
您可以使用正则表达式来实现这一点,例如:

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Example {

    public static void main(String[] args) {

        String data = "java example \"this is a test\" hello world";

        Pattern p = Pattern.compile("((?:\"[a-z\\s]+\")|[a-z]+)");
        Matcher m = p.matcher(data);

        List<String> lst = new ArrayList<String>();
        while(m.find()) {
            lst.add(m.group(1));
        }

        String[] result= new String[lst.size()];
        result = lst.toArray(results);

        for(String s: result) {
            System.out.println(s);
        }
    }
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.regex.Pattern;
导入java.util.regex.Matcher;
公开课范例{
公共静态void main(字符串[]args){
String data=“java example\”这是一个测试\“hello world”;
Pattern p=Pattern.compile(((?:\“[a-z\\s]+\”)|[a-z]+)”;
匹配器m=p.Matcher(数据);
List lst=new ArrayList();
while(m.find()){
第一次添加(m组(1));
}
字符串[]结果=新字符串[lst.size()];
结果=第一天(结果);
for(字符串s:结果){
系统输出打印项次;
}
}
}
正则表达式
((?:\“[a-z\\s]+\”)|[a-z]+)
将在以下任一项上匹配: 1) 字符序列
a
z
或双引号之间的空格 2) 字符序列
a
z


然后我们使用
m提取这些匹配项。find

我认为您有点困惑,代码中有错误! 组成字符串应为:

String text = "java example \"this is a test\" hello world";
变量
text
的值将是:

java example "this is a test" hello world
我假设您希望将其提取到以下数组中:

result[0] = "java";
result[1] = "example";
result[2] = "\"this is a test\"";
result[3] = "hello";
result[4] = "world";
您可以使用正则表达式来实现这一点,例如:

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Example {

    public static void main(String[] args) {

        String data = "java example \"this is a test\" hello world";

        Pattern p = Pattern.compile("((?:\"[a-z\\s]+\")|[a-z]+)");
        Matcher m = p.matcher(data);

        List<String> lst = new ArrayList<String>();
        while(m.find()) {
            lst.add(m.group(1));
        }

        String[] result= new String[lst.size()];
        result = lst.toArray(results);

        for(String s: result) {
            System.out.println(s);
        }
    }
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.regex.Pattern;
导入java.util.regex.Matcher;
公开课范例{
公共静态void main(字符串[]args){
String data=“java example\”这是一个测试\“hello world”;
Pattern p=Pattern.compile(((?:\“[a-z\\s]+\”)|[a-z]+)”;
匹配器m=p.Matcher(数据);
List lst=new ArrayList();
while(m.find()){
第一次添加(m组(1));
}
字符串[]结果=新字符串[lst.size()];
结果=第一天(结果);
for(字符串s:结果){
系统输出打印项次;
}
}
}
正则表达式
((?:\“[a-z\\s]+\”)|[a-z]+)
将在以下任一项上匹配: 1) 字符序列
a
z
或双引号之间的空格 2) 字符序列
a
z


然后,我们使用
m.find

提取这些匹配项。谢谢。你说得对。我只是编写了代码,没有在编译器上测试。谢谢。你说得对。我只是编写了代码,没有在编译器上测试。