简体   繁体   English

Java-根据空格和单引号拆分字符串,但忽略两个单引号

[英]Java - Split string based on space and single quote but ignore two single quotes

I want to split the string using space and single quote. 我想使用空格和单引号分隔字符串。 But two single quotes should be ignored. 但是应该忽略两个单引号。

Input: 输入:

name eq 'a''b'

Output should be: 输出应为:

name

eq

a''b

I tried to use 我尝试使用

[^\\s\"(?<!')'(?!')]+|\"[^\"]*\"|(?<!')'(?!')[^(?<!')'(?!')]*(?<!')'(?!')"

but it does not work. 但它不起作用。

The following one should suit your needs: 以下内容将满足您的需求:

'(?:[^']|'')+'|[^ ]+

正则表达式可视化

Debuggex Demo Debuggex演示


Java example: Java示例:

String input = "foobar foo bar 'foobar' 'foo bar' 'foo''bar'";
Pattern pattern = Pattern.compile("'(?:[^']|'')+'|[^ ]+");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    String match = matcher.group();
    System.out.println(match);
}

Ideone Demo Ideone演示

Hi the actual regex is 嗨,实际的正则表达式是

"\\s|(?<!')'(?!')"

It produces output array as follows 它产生输出数组如下

[name, eq, , a''b]

This can be done without regex too. 也可以不用regex来完成。

String s = "name eq 'a''b'";
String[] split = new String[3];

Scanner in = new Scanner(s);

split[0] = in.next();
split[1] = in.next();

String temp = in.next();
split[2] = temp.substring(1,temp.length() - 1);

//test output
for(String x : split)
    System.out.println(x);

This will print: 这将打印:

name
eq
a''b

If you need a more generic solution you need to provide more details. 如果您需要更通用的解决方案,则需要提供更多详细信息。
Hope this helps. 希望这可以帮助。

Here is how I would do: 这是我的做法:

String value = "name eq 'a''b'";
value = value.replaceAll(" '", " ").replaceAll("'$", "");
System.out.println(value);
String[] arr = value.split(" ");

You can try with Lookaround 您可以尝试环视

Pattern 图案

(?<!')'(?!')

Sample code: 样例代码:

System.out.println(Arrays.toString("name eq 'a''b'".split("(?<!')'(?!')")));

output: 输出:

[name eq , a''b] 

Pattern explanation: 模式说明:

  (?<!                     look behind to see if there is not:
    '                        '\''
  )                        end of look-behind
  '                        '\''
  (?!                      look ahead to see if there is not:
    '                        '\''
  )                        end of look-ahead

To split based on space or single quote use 要根据空间或单引号进行拆分,请使用

 *(?<!')[ '](?!') *

sample code: 样例代码:

System.out.println(Arrays.toString("name eq 'a''b'".split(" *(?<!')[ '](?!') *"))); 

output: 输出:

[name, eq, a''b]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用java中的两个单引号替换字符串中的单引号? - How to replace single quote within a string with two single quotes in java? 如何在Java String中用单个双引号替换两个双引号? - How to replace two double quotes with a single double quote in Java String? 字符串拆分单引号 - String split single quotes JAVA基于分隔符在String中添加单引号 - JAVA Adding a Single quote in String based on the delimeter 使用单引号作为分割点将字符串拆分为子字符串,但不带引号,其后跟反斜杠(\\),然后再加上另一个引号 - Split a string into substring using single quote as a split point but not quotes preceded by backslash(\) and followed by another quote 基于空格在java中拆分字符串,转义双引号和单引号中的空格以及以\\开头的空格 - Split a string in java based on white spaces escaping those spaces in double quotes and single quotes and that which are preceded by \ 在单个空格处拆分字符串 - Split a string at a single space 在创建文件名时,java中的单引号自动替换为两个单引号 - Single quote is being replaced by two single quotes automatically in java while creating a filename 基于空格和Java中匹配引号的正则表达式拆分字符串 - Regular Expression to Split String based on space and matching quotes in java Java - 替换单引号而不影响多个单引号 - Java - Replace single quote without affecting multi single quotes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM