【问题标题】:RegEx for finding matches between quotesRegEx 用于查找引号之间的匹配项
【发布时间】:2019-10-17 15:54:14
【问题描述】:

在这里我试图在double quotes 中找到String

   List<String> getList(String value){
    String regex = "\"[^\"]*\"|[^,]+";
    List<String> allMatches = new ArrayList<String>();
    if (StringUtils.isNotBlank(value)) {
        Matcher m = Pattern.compile(regex).matcher(value);
        while (m.find() && StringUtils.isNotBlank(m.group())) {
            String str=m.group().replaceAll("^\"|\"$", "");
            allMatches.add(str.trim());
        }
    }
    return allMatches;
  }

  result = getList(400,test,\"don't split, this\",15);
  result have [400,test,don't split, this,15] all comma seperated string except inside quotes.

它适用于模式 "" 但不适用于 “”"foo,bar",不同于"foo,bar" here is not working regex

【问题讨论】:

  • 将第一个和第二个\" 替换为[\"“],将最后一个\" 替换为[\"”]
  • @PaulLemarchand 这会起作用,但它也会匹配混合引号,例如"HELLO”.
  • 可能是因为这个引号是不同的字符:" U+0022, ” U+201C, ” U+201D。
  • @TimBiegeleisen 我相信如果他想在两个引号之间查找字符串,"...” 符合这个要求。

标签: java regex regex-lookarounds regex-negation regex-greedy


【解决方案1】:

如果不同的引号应该匹配但不应混合,您可以使用alternation 来匹配任何一种格式。如果您不想匹配换行符,可以将其添加到否定字符类中。

(?:“[^“”]+”|"[^"]+"|(?<=,|^)[^“”,"]+(?=(?:,|$)))

说明

  • (?:非捕获组
    • “[^“”]+” 匹配变体 ,然后不匹配 并匹配变体
    • |或者
    • "[^"]+" 匹配,",然后不是 ""
    • |或者
    • (?&lt;=,|^)断言左边是逗号或字符串开头
    • [^“”,"]+匹配任何不在字符类中的字符
    • (?=(?:,|$)) 断言右边是逗号或字符串结尾
  • )关闭非捕获组

Regex demo | Java demo

整个模式是一个交替,有 3 个选项。前 2 个选项从开盘价到收盘价相匹配。

第三个选项匹配除引号或逗号之外的所有类型,但确保在匹配的开头和结尾处有逗号或字符串的开头或结尾。

【讨论】:

  • 是的,该模式正确捕获了""“” 引号!
  • @第四只鸟现在错过了那些用逗号分隔的字符串
  • 有没有例子:
  • @Thefourthbird stackoverflow.com/questions/18893390/… 看看这个,这正是我想要的,但在我的情况下,RegEx 在某些情况下无法这样做
  • 您是否尝试过更新的模式"[^",]+"|“[^“”,]+” 在这种情况下,我认为您可以将那些对您的问题不起作用的情况添加到您的问题中。
【解决方案2】:

您可以使用 Java 代码进行 CSV 样式混合,但必须更改正则表达式。

Java

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{

    public static List<String> getList(String value)
    {
        String regex = "(?:(?:^|,|\\r?\\n)\\s*)(?:(?:(\"[^\"\\\\]*(?:\\\\[\\S\\s][^\"\\\\]*)*\"|“[^“”\\\\]*(?:\\\\[\\S\\s][^“”\\\\]*)*”))(?:\\s*(?:(?=,|\\r?\\n)|$))|([^,]*)(?:\\s*(?:(?=,)|$)))"; 
        List<String> allMatches = new ArrayList<String>();
        if ( value.length() > 0  )
        {
            Matcher m = Pattern.compile( regex ).matcher( value );
            while ( m.find() ) {
                String str = m.group(2);
                if ( str == null ) {
                    str = m.group(1);
                    str = str.replaceAll( "^[\"“”]|[\"“”]$", "" );
                }
                allMatches.add(str.trim());
            }
        }
        return allMatches;
    }


    public static  void main (String[] args) throws java.lang.Exception
    {
        List<String>  result = getList("400,test,\"QT_don't split, this_QT\",15");
        System.out.println( result );

        result = getList("500,test,“LQT_don't split, this_RQT”,15");
        System.out.println( result );

        result = getList("600,test,\"QT_don't split, this_QT\",15");
        System.out.println( result );

    }
}

https://ideone.com/b8Wnz9

输出

[400, test, QT_don't split, this_QT, 15]
[500, test, LQT_don't split, this_RQT, 15]
[600, test, QT_don't split, this_QT, 15]

正则表达式扩展

 (?:
      (?: ^ | , | \r? \n )          # Delimiter comma or newline
      \s*                           # leading optional whitespaces
 )
 (?:                           # Double Quoted field
      (?:
           "                             # Quoted string field ""
           (                             # (1), double quoted string data
                [^"\\]* 
                (?: \\ [\S\s] [^"\\]* )*
           )
           "

        |                              # or

           “                             # Quoted string field Left/right double quotes “”   
           (                             # (2), double quoted string data
                [^“”\\]* 
                (?: \\ [\S\s] [^“”\\]* )*
           )
           ”
      )
      (?:
           \s*                           # trailing optional whitespaces
           (?:
                (?= , | \r? \n )              # Delimiter ahead, comma or newline
             |  $ 
           )
      )
   |                              # OR
      ( [^,]* )                     # (3), Non quoted field
      (?:
           \s*                           # trailing optional whitespaces 
           (?:
                (?= , )                       # Delimiter ahead, comma
             |  $ 
           )
      )
 )

【讨论】:

    【解决方案3】:

    试试这个:

    Pattern regex = Pattern.compile("[\"\u201C](.*)[\"\u201D]");
    List<> allMatches = new ArrayList<String>();
    Matcher m = regex.matcher(value);
    while (m.find()) {
        allMatches.add(m.group(1).trim());
    }
    

    简单得多,并且完全符合您的要求(匹配普通引号或“漂亮”引号中的内容,但如果您混合使用它们或无法启动或关闭它们,则不会)。

    【讨论】:

      猜你喜欢
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 2011-01-30
      相关资源
      最近更新 更多