忽略引号和转义引号中的空格

忽略引号和转义引号中的空格

本文介绍了Java Regex-在空格处分割字符串-忽略引号和转义引号中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找正则表达式以在Java中执行以下操作:

I'm looking for regex to do the following in Java:

String originalString = "";
String splitString[] = originalString.spilt(regex);

一些测试用例:

Original1: foo bar "simple"
Spilt1: { "foo", "bar", "\"simple\"" }

Original2: foo bar "harder \"case"
Spilt2: { "foo", "bar", "\"harder \"case\"" }

Original3: foo bar "harder case\\"
Spilt3: { "foo", "bar", "\"harder case\\"" }

我遇到过一些片段:

# Does not react to escaped quotes
 (?=([^\"]*\"[^\"]*\")*[^\"]*$)
# Finds relevant quotes that surround args
(?<!\\)(?:\\{2})*\"

谢谢!

推荐答案

String stringToSplit = This is the string to split;

String[] split = stringToSplit.split("character to split at");

在这种情况下, split [0] 结果将为'This', split [1] 将为'is', split [2] 将为' ', split [3] '字符串' split [4] '到' split [5] 'split'。

In this case, split[0] would result as ' This ', split[1] would be ' is ', split[2] would be ' the ', split[3] ' string ' split[4] ' to ' split[5] ' split '.

此时您可以执行

var0 = split[0];
var1 = split[1];
var2 = split[2];

其中 var0 等于 This
依此类推...

Where var0 would equal "This"And so on...

希望这会有所帮助。

这篇关于Java Regex-在空格处分割字符串-忽略引号和转义引号中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:49