java - RegEx 用于查找引号之间的匹配项
问题描述
解决方案
您可以使用 Java 代码进行 CSV 样式混合,但必须更改正则表达式。
爪哇
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 );
}
}
输出
[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
| $
)
)
)
推荐阅读
- c# - DocuSign 签名不会取代 usign Rest API PDF 表单字段
- django - Django 模型设计 - ManyToManyField 或 ForeignKey:选择选项
- python - 即使什么都没有,我的脚本也会继续打印
- python - 在python中的特定字符之前删除空格?
- python - 如何使用列表理解获得给定列表的所有元素的总和?
- android - 如何使用 minSDK = 21 在 Android/Kotlin 上处理日期?
- docker - Docker 用 yarn monorepo 撰写文件
- ios - 带有 .zero 框架的视图的位置
- javascript - 如何仅在单击具有特定 ID 的按钮时触发事件
- multithreading - 嵌入式网络服务器