[英]Regular expression to match exact string with with non-numeric characters and variable number
I have been struggling with this and would appreciate any help.我一直在努力解决这个问题,并希望得到任何帮助。
I have a line of code that always looks like this:我有一行代码总是看起来像这样:
Thread.sleep(1000)
Thread.sleep()
is always a fixed sequence of characters, but the integer number inside is variable. Thread.sleep()
总是一个固定的字符序列,但里面的整数是可变的。 How would I write a regular expression to catch this?我将如何编写正则表达式来捕捉这个?
You can use the regex , Thread\\s*\\.\\s*sleep\\s*\\(\\s*\\d+\\s*\\)
.您可以使用正则表达式
Thread\\s*\\.\\s*sleep\\s*\\(\\s*\\d+\\s*\\)
。
Explanation of the regex:正则表达式的解释:
Thread
: Literal, Thread
Thread
:文字, Thread
\\s*\\.\\s*
: .
\\s*\\.\\s*
: .
preceded by and followed by 0+ whitespace characterssleep\\s*
: Literal, sleep
followed by 0+ whitespace characters sleep\\s*
:文字, sleep
后跟 0+ 空格字符\\(\\s*
: The character, (
followed by 0+ whitespace characters \\(\\s*
: 字符, (
后跟 0+ 空格字符\\d+\\s*
: One or more digits followed by 0+ whitespace characters \\d+\\s*
:一位或多位数字后跟 0+ 空格字符\\)
: The character, )
\\)
: 字符, )
Demo:演示:
public class Main {
public static void main(String[] args) {
String x = "Some Thread.sleep(1000) text";
x = x.replaceAll("Thread\\s*\\.\\s*sleep\\s*\\(\\s*\\d+\\s*\\)", "");
System.out.println(x);
}
}
Output:输出:
Some text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.