简体   繁体   English

仅删除Java字符串中的X个空格

[英]Only removing X spaces in a Java String

So I'm parsing files of the following form (A and B not included in format) for "Item #", adding them to a list, given .'s are spaces for clarity: 因此,我将为“项目#”解析以下格式的文件(格式不包括A和B),将它们添加到列表中,给定。是为了清楚起见:

Someword: a list of words of any length      (A)
....Item 1                                   (B)
....Item 2
.Item 3

Where section (A) is always of that form, and section B is always indented with either a tab (4 spaces) or a single space. 其中(A)节始终是这种形式,而B节总是以制表符(4个空格)或单个空格缩进。 My result is {Item 1,Item 2,Item 3}. 我的结果是{Item 1,Item 2,Item 3}。 Up until now, I've just used a regex to match the (A) section, then added the following lines with .trim() called on them. 到目前为止,我只使用了一个正则表达式来匹配(A)部分,然后在它们上面添加了带有.trim()的以下行。

My question is, how would I go about parsing through something that looks like this: 我的问题是,我将如何解析如下所示的内容:

Someword: a list of words of any length 
........Item 1

Such that the second line has 8 spaces. 这样第二行有8个空格。 So I want to ignore the first 4 (or potentially 1) spaces, and capture everything else, resulting in {....Item 1}, if x's are spaces in this case. 因此,在这种情况下,如果x是空格,那么我想忽略前4个(或可能是1个)空格,并捕获其他所有内容,从而产生{.... Item 1}。

You can use one regex and get all items with on single pass: 您可以使用一个正则表达式,并一次性获得所有项目:

Code

String str = "Someword: a list of words of any length\r\n" +
             "    Item 1\r\n" + // 4 spaces at the beginning
             "    Item 2\r\n" + // 4 spaces at the beginning
             " Item 3\r\n" + // 1 space at the beginning
             "        Item 4\r\n"; // 8 spaces at the beginning

Pattern p = Pattern.compile("(?m)^\\s+(Item\\s+\\d+)$");

Matcher m = p.matcher(str);
while(m.find()) {
    System.out.println(m.group(1));
}

Output 输出量

Item 1
Item 2
Item 3
Item 4

Description 描述

正则表达式可视化

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM