Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.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
拆分字符串未包含在java中的字符串_Java_Arrays_Regex_String_Split - Fatal编程技术网

拆分字符串未包含在java中的字符串

拆分字符串未包含在java中的字符串,java,arrays,regex,string,split,Java,Arrays,Regex,String,Split,如何使用split cretiria拆分以下文本:首先,现在,然后: String text = "FIRST i go to the homepage NOW i click on button \"NOW CLICK\" very quick THEN i will become a text result."; 以下是三句话: 首先,我进入主页 现在我点击按钮“现在点击”非常快 然后我将成为一个文本结果 此代码不工作,因为按钮“NOW CLICK”

如何使用split cretiria拆分以下文本:首先,现在,然后:

String text = "FIRST i go to the homepage NOW i click on button \"NOW CLICK\" very quick THEN i will become a text result.";
以下是三句话:

  • 首先,我进入主页
  • 现在我点击按钮“现在点击”非常快
  • 然后我将成为一个文本结果
  • 此代码不工作,因为按钮“NOW CLICK”


    您可以使用模式和匹配器使用组分割输入:

    Pattern pattern = Pattern.compile("^(FIRST.*?)(NOW.*?)(THEN.*)$");
    
    String text = "FIRST i go to the homepage NOW i click on button \"NOW CLICK\" very quick THEN i will become a text result.";
    
    Matcher matcher = pattern.matcher(text);
            
    if (matcher.find()) {
        System.out.println(matcher.group(1));
        System.out.println(matcher.group(2));
        System.out.println(matcher.group(3));
    }
    
    输出:

    FIRST i go to the homepage 
    NOW i click on button "NOW CLICK" very quick 
    THEN i will become a text result.
    
    FIRST i go to the homepage
     NOW i click on button "NOW CLICK" very quick 
    THEN i will become a text result.
    
    FIRST i go to the homepage 
    NOW i click on button "NOW CLICK" very quick 
    THEN i will become a text result.
    
    您可以使用以下选项:

    输出:

    FIRST i go to the homepage 
    NOW i click on button "NOW CLICK" very quick 
    THEN i will become a text result.
    
    FIRST i go to the homepage
     NOW i click on button "NOW CLICK" very quick 
    THEN i will become a text result.
    
    FIRST i go to the homepage 
    NOW i click on button "NOW CLICK" very quick 
    THEN i will become a text result.
    

    您需要使用lookaheads和lookback(简要提及)

    只需将方法中的正则表达式更改为以下内容即可:

    String[] textArray = text.split("((?=FIRST)|(?=NOW(?! CLICK))|(?=THEN))");
    
    甚至在每个表达式中包含一个空格可能更好,以防止在以下情况下拆分,例如,无处拆分:

    String[] textArray = text.split("((?=FIRST )|(?=NOW (?!CLICK))|(?=THEN ))");
    

    如果我没听错的话,你

    • 要先将关键字
      上的文本分隔开
      现在
      然后
      并将它们保留在结果部分中吗
    • 但是,如果这些关键字出现在引号内,我不想对它们进行拆分
    如果我的猜测是正确的,而不是使用
    split
    方法,您可以使用
    find
    对所有对象进行迭代

    • 引用
    • 不在引号内的单词
    • 空白
    这将允许您将所有引号和空格添加到结果中,并且只关注检查不在引号内的单词,以查看是否应该拆分它们

    表示这些部分的正则表达式可以类似于
    Pattern.compile(“\”[^\“]*\“\\\\S+\\\S+”)

    重要:我们需要搜索“.”。首先,否则
    \\S+
    也会匹配
    “现在单击“
    ”作为
    “现在”
    单击“
    ”作为两个单独的部分,这将防止它被视为单个引号。这就是为什么我们要放置
    “[^”]*”
    正则表达式(表示引号)在
    subsectex1 | subsectex2 | subsectex3
    系列的开始处

    这个正则表达式允许我们迭代文本

    首先我进入主页,现在我很快点击“NOW click”按钮,然后我将成为一个文本结果。

    作为代币

    FIRST
    i
    go
    
    
    主页
    现在
    i
    单击
    开启
    按钮
    “现在单击”
    非常
    quick
    然后
    i
    变成
    a
    text
    结果。
    然后
    i
    变成
    a
    text
    结果。

    请注意,
    “现在单击”
    将被视为单个标记。因此,即使它将包含要拆分的内部关键字,它也永远不会等于该关键字(因为它将包含其他字符,如“
    ,或仅包含引号中的其他单词)。这将防止它被视为应拆分文本的分隔符

    利用这个想法,我们可以创建如下代码:

    String text = "FIRST i go to the homepage NOW i click on button \"NOW CLICK\" very quick THEN i will become a text result.";
    List<String> keywordsToSplitOn = List.of("FIRST", "NOW", "THEN");
    
    //lets search for quotes ".." | words | whitespaces
    Pattern p = Pattern.compile("\"[^\"]*\"|\\S+|\\s+");
    Matcher m = p.matcher(text);
    
    StringBuilder sb = new StringBuilder();
    List<String> result = new ArrayList<>();
    while(m.find()){
        String token = m.group();
        if (keywordsToSplitOn.contains(token) && sb.length() != 0){
            result.add(sb.toString());
            sb.delete(0, sb.length());//clear sb
        }
        sb.append(token);
    }
    if (sb.length() != 0){//include rest of text after last keyword 
        result.add(sb.toString());
    }
    
    result.forEach(System.out::println);
    

    您可以匹配以下正则表达式

    /\bFIRST +(?:(?!\bNOW\b)[^\n])+(?<! )|\bNOW +(?:(?!\bTHEN\b)[^\n])+(?<! )|\bTHEN +.*/
    
    /\bFIRST+(?:(?!\bNOW\b)[^\n])+(?
    

    Java的正则表达式引擎执行以下操作

    \bFIRST +      : match 'FIRST' preceded by a word boundary,
                     followed by 1+ spaces
    (?:            : begin a non-capture group
      (?!\bNOW\b)  : use a negative lookahead to assert that
                     the following chars are not 'NOW'  
      [^\n]        : match any char other than a line terminator
    )              : end non-capture group
    +              : execute non-capture group 1+ times
    (?<! )         : use negative lookbehind to assert that the
                     previous char is not a space
    |              : or
    \bNOW +        : match 'NOW' preceded by a word boundary,
                     followed by 1+ spaces
    (?:            : begin a non-capture group
      (?!\bTHEN\b) : use a negative lookahead to assert that
                     the following chars are not 'THEN'  
      [^\n]        : match any char other than a line terminator
    )              : end non-capture group
    +              : execute non-capture group 1+ times
    (?<! )         : use negative lookbehind to assert that the
                     previous char is not a space
    |              : or
    \bTHEN +.*     : match 'THEN' preceded by a word boundary,
                     followed by 1+ spaces then 0+ chars
    
    \bFIRST+:匹配前面有单词边界的“FIRST”,
    后跟1+空格
    (?::开始一个非捕获组
    (?!\bNOW\b):使用负前瞻来断言
    以下字符不是“现在”
    [^\n]:匹配除行终止符以外的任何字符
    ):结束非捕获组
    +:执行非捕获组1+次
    (?
    
    除了处理引用的文本,
    String.split()之外,它还使用了一种称为.

    的技术
    删除作为分隔符的匹配文本,因此你永远不会得到
    第一次我进入主页
    -你会得到
    我进入主页
    ,现在仍然在第二次分裂。有趣的是,这是公认的答案,因为它给这个简单的问题增加了如此多的复杂性。@JWoodchuck我不认为主要的区别令人遗憾的是,“这个解决方案更复杂”,但它使用了与其他答案不同的假设(在我之前发布)例如,你的解决方案是基于这样一个假设,即OP不想在
    NOW
    上拆分,后面跟着
    单击
    ,而我的解决方案是OP不想在
    NOW
    上拆分,后者放在引号内。我们的解决方案可能会对当前OP示例给出相同的结果,但对其他句子l的效果不同ike
    首先选择选项A。现在单击按钮B。
    。这是有意义的。我想这取决于他们更广泛的需求,以及其他因素,如复杂性和引入不同的(非拆分)选项方法。@JWoodchuck是的。我假设这可能是OP想做某事的另一种情况,但只描述了一个方面/过于简化的情况,并显示了他失败的尝试(这里使用
    拆分
    )。通常,问题中显示的尝试对OP来说不是强制性的,即使它是OP所问问题的一部分。