拆分空格列表和分组引号字符
Split a list on spaces and group quoted characters
我正在尝试将输入字符串解析为标记,其中每个标记都是字符串中的一个词。
但是,我也希望标记能够包含空格,并且为了更清晰的语法,我希望能够让引号出现在标记的中间,并且能够转义引号 (\"
)
示例输入字符串和我想要的输出(从输出中删除引号以表示字符串以提高可读性):
- 输入:
diamond_sword name:"test name"
-> 输出:[diamond_sword, name:test 名称]
- 输入:
stick 1 name:"The \"Holy\" Stick"
-> 输出:[stick, 1, name:The "Holy" Stick]
我不希望引号必须与其他单词分开 (name:"string"
),而不是按照许多其他人在之前的问题中提出的要求进行操作,而我只想 转义 保留引号,删除所有未转义的引号。
这可能吗?以这种方式将字符串转换为列表会是什么样子?
也许是这样的?
import java.util.*;
public class Demo {
private static List<String> parse(String in) {
Objects.requireNonNull(in);
char[] chars = in.toCharArray();
var words = new ArrayList<String>();
var sb = new StringBuilder();
for (int i = 0; i < chars.length; i++) {
if (chars[i] == ' ') {
// Space; add the current token to the result array.
words.add(sb.toString());
sb.setLength(0);
} else if (chars[i] == '"') {
// Iterate until the next unescaped quote
// (Assumes strings are well-formatted; a more robust version
// wouldn't and would better handle error cases)
for (i++; chars[i] != '"'; i++) {
// If current character is a backslash, skip and append
// the next
if (chars[i] == '\') {
i++;
}
sb.append(chars[i]);
}
} else {
sb.append(chars[i]);
}
}
words.add(sb.toString()); // Don't forget the final token
return words;
}
public static void main(String[] args) {
List<String> strings =
List.of("diamond_sword name:\"test name\"",
"stick 1 name:\"The \\"Holy\\" Stick\"");
for (String s : strings) {
List<String> words = parse(s);
System.out.println(words);
}
}
}
编译后 运行,打印出来
[diamond_sword, name:test name]
[stick, 1, name:The "Holy" Stick]
我正在尝试将输入字符串解析为标记,其中每个标记都是字符串中的一个词。
但是,我也希望标记能够包含空格,并且为了更清晰的语法,我希望能够让引号出现在标记的中间,并且能够转义引号 (\"
)
示例输入字符串和我想要的输出(从输出中删除引号以表示字符串以提高可读性):
- 输入:
diamond_sword name:"test name"
-> 输出:[diamond_sword, name:test 名称] - 输入:
stick 1 name:"The \"Holy\" Stick"
-> 输出:[stick, 1, name:The "Holy" Stick]
我不希望引号必须与其他单词分开 (name:"string"
),而不是按照许多其他人在之前的问题中提出的要求进行操作,而我只想 转义 保留引号,删除所有未转义的引号。
这可能吗?以这种方式将字符串转换为列表会是什么样子?
也许是这样的?
import java.util.*;
public class Demo {
private static List<String> parse(String in) {
Objects.requireNonNull(in);
char[] chars = in.toCharArray();
var words = new ArrayList<String>();
var sb = new StringBuilder();
for (int i = 0; i < chars.length; i++) {
if (chars[i] == ' ') {
// Space; add the current token to the result array.
words.add(sb.toString());
sb.setLength(0);
} else if (chars[i] == '"') {
// Iterate until the next unescaped quote
// (Assumes strings are well-formatted; a more robust version
// wouldn't and would better handle error cases)
for (i++; chars[i] != '"'; i++) {
// If current character is a backslash, skip and append
// the next
if (chars[i] == '\') {
i++;
}
sb.append(chars[i]);
}
} else {
sb.append(chars[i]);
}
}
words.add(sb.toString()); // Don't forget the final token
return words;
}
public static void main(String[] args) {
List<String> strings =
List.of("diamond_sword name:\"test name\"",
"stick 1 name:\"The \\"Holy\\" Stick\"");
for (String s : strings) {
List<String> words = parse(s);
System.out.println(words);
}
}
}
编译后 运行,打印出来
[diamond_sword, name:test name]
[stick, 1, name:The "Holy" Stick]