如何使用Java正则表达式API中的Pattern和Matcher来删除特定行

huangapple go评论84阅读模式
英文:

How to use Pattern, Matcher in Java regex API to remove a specific line

问题

["-122","+","2","?","s","a","b"]
英文:

I have a complicate string split, I need to remove the comments, spaces, and keep all the numbers but change all string into character. If the - sign is at the start and followed by a number, treat it as a negative number rather than a operator

the comment has the style of ?<space>comments<space>? (the comments is a place holder)

Input :

-122+2 ? comments ?sa b
-122+2 ? blabla ?sa b

output :

 ["-122","+","2","?","s","a","b"]  

(all string into character and no space, no comments)

答案1

得分: 2

  1. 使用正则表达式 \\s*\\?\\s*\\w+\\s*(?=\\?) 将不需要的字符串替换为 ""。您可以链接 String#replaceAll 来删除任何剩余的空白字符。注意,?= 表示正向预查,意思是 \s*\?\s*\w+\s* 后跟一个 ?。我希望您已经知道 \s 表示空白字符,\w 表示单词字符。
  2. 然后您可以使用正则表达式 ^-\d+|\d+|\D,它表示开头的负整数(即 ^-\d+),或者数字(即 \d+),或者非数字字符(\D)。

示例代码:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String str = "-122+2 ? comments ?sa b";
        str = str.replaceAll("\\s*\\?\\s*\\w+\\s*(?=\\?)", "").replaceAll("\\s+", "");

        Pattern pattern = Pattern.compile("^-\\d+|\\d+|\\D");
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.group());
        }
    }
}

输出结果:

-122
+
2
?
s
a
b
英文:
  1. Replace the unwanted string \s*\?\s*\w+\s*(?=\?) with "". You can chain String#replaceAll to remove any remaining whitespace. Note that ?= means positive lookahead and here it means \s*\?\s*\w+\s* followed by a ?. I hope you already know that \s specifies whitespace and \w specifies a word character.
  2. Then you can use the regex, ^-\d+|\d+|\D which means either negative integer in the beginning (i.e. ^-\d+) or digits (i.e. \d+) or a non-digit (\D).

Demo:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
	public static void main(String[] args) {
		String str = "-122+2 ? comments ?sa b";
		str = str.replaceAll("\\s*\\?\\s*\\w+\\s*(?=\\?)", "").replaceAll("\\s+", "");

		Pattern pattern = Pattern.compile("^-\\d+|\\d+|\\D");
		Matcher matcher = pattern.matcher(str);
		while (matcher.find()) {
			System.out.println(matcher.group());
		}
	}
}

Output:

-122
+
2
?
s
a
b

huangapple
  • 本文由 发表于 2020年10月19日 06:34:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64418988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定