Java 正则表达式 - 如何替换以开头和结尾的模式
Java Regex - How to replace a pattern starts with and ends with
我有 2 个场景:
- 字符串以示例国家/地区开头!即 样本国家!测试数据
我想要一个正则表达式来替换样本国家!带空字符串,这里的国家不固定,可以是美国,法国等
我试过了:
System.out.println(str.replaceAll("^(Sample[^!]+!)", ""));
我正在获取输出
! Test Data
而我只想
Test Data
字符串以示例国家/地区结尾!即 测试数据样本国家 !
我也在这里
测试数据
谁能帮忙提供正确的正则表达式和解释。非常感谢
编辑:
让我们做一个更好的方法。您将不仅有 2 个案例,您将有 3 个案例
(模式+数据) ---> ^Sample[^!]+! (模式) ([^!]) (数据)
(数据+模式) ---> ([^!]) (数据) 样本[ ^!]+!$ (模式)
(模式+数据+模式) ---> (^Sample[^!]+!(模式) ([^!]) (数据) 样本[^!]+!$ (模式)
因此我们必须使用正则表达式检查字符串中的所有情况。我们需要正则表达式中的 OR 案例是“|”另一件事是我们必须避免不匹配的情况必须被忽略它是 (?:(regex)) descripted here
public class HelloWorld {
public static void main(String[] args) {
String[] testcases = new String[] {
"Sample foo ! Test1 Data",
"Sample bar ! Test2 Data",
"Test3 Data Sample foo !",
"Test4 Data Sample bar !",
"Sample bar ! Test5 Data Sample bar !"
};
for (String str: testcases) {
System.out.println(str.replaceAll("(?:(^Sample[^!]+!([^!])))|(?:(([^!])Sample[^!]+!$))|(?:(^Sample[^!]+!([^!]))Sample[^!]+!$)", "").trim());
}
}
}
我们使用了您的正则表达式,并在数据分组后制作了一个新的正则表达式,数据将位于 ($2,$4) 组,因为我们用第二组和第四组值替换了字符串。我希望这将有所帮助。
compile code online
在这里试试这个正则表达式:
String[] testcases = new String[] {
"Sample foo ! Test Data",
"Sample bar ! Test Data",
"Test Data Sample foo !",
"Test Data Sample bar !"
};
for (String str : testcases) {
System.out.println(str.replaceAll("(.* ?)(Sample[a-zA-Z ]+ ! ?)(.*)", ""));
}
解释:
(.* ?) // first match group, matches anything, followed by an optional space
(Sample[a-zA-Z ]+ ! ?) // second match group, matches the String "Sample followed by a series of letters (your country), a whitespace, an exclamation mark and an optional space
(.*) // third match group, matches anything
因此第二个匹配组 ($2) 将包含您的 "Sample Country" 字符串,我们可以只用第一个 ($1) 和第三个 ($3) 匹配组替换结果。
我有 2 个场景:
- 字符串以示例国家/地区开头!即 样本国家!测试数据
我想要一个正则表达式来替换样本国家!带空字符串,这里的国家不固定,可以是美国,法国等
我试过了:
System.out.println(str.replaceAll("^(Sample[^!]+!)", ""));
我正在获取输出
! Test Data
而我只想
Test Data
字符串以示例国家/地区结尾!即 测试数据样本国家 ! 我也在这里
测试数据
谁能帮忙提供正确的正则表达式和解释。非常感谢
编辑:
让我们做一个更好的方法。您将不仅有 2 个案例,您将有 3 个案例
(模式+数据) ---> ^Sample[^!]+! (模式) ([^!]) (数据)
(数据+模式) ---> ([^!]) (数据) 样本[ ^!]+!$ (模式)
(模式+数据+模式) ---> (^Sample[^!]+!(模式) ([^!]) (数据) 样本[^!]+!$ (模式)
因此我们必须使用正则表达式检查字符串中的所有情况。我们需要正则表达式中的 OR 案例是“|”另一件事是我们必须避免不匹配的情况必须被忽略它是 (?:(regex)) descripted here
public class HelloWorld {
public static void main(String[] args) {
String[] testcases = new String[] {
"Sample foo ! Test1 Data",
"Sample bar ! Test2 Data",
"Test3 Data Sample foo !",
"Test4 Data Sample bar !",
"Sample bar ! Test5 Data Sample bar !"
};
for (String str: testcases) {
System.out.println(str.replaceAll("(?:(^Sample[^!]+!([^!])))|(?:(([^!])Sample[^!]+!$))|(?:(^Sample[^!]+!([^!]))Sample[^!]+!$)", "").trim());
}
}
} 我们使用了您的正则表达式,并在数据分组后制作了一个新的正则表达式,数据将位于 ($2,$4) 组,因为我们用第二组和第四组值替换了字符串。我希望这将有所帮助。 compile code online
在这里试试这个正则表达式:
String[] testcases = new String[] {
"Sample foo ! Test Data",
"Sample bar ! Test Data",
"Test Data Sample foo !",
"Test Data Sample bar !"
};
for (String str : testcases) {
System.out.println(str.replaceAll("(.* ?)(Sample[a-zA-Z ]+ ! ?)(.*)", ""));
}
解释:
(.* ?) // first match group, matches anything, followed by an optional space
(Sample[a-zA-Z ]+ ! ?) // second match group, matches the String "Sample followed by a series of letters (your country), a whitespace, an exclamation mark and an optional space
(.*) // third match group, matches anything
因此第二个匹配组 ($2) 将包含您的 "Sample Country" 字符串,我们可以只用第一个 ($1) 和第三个 ($3) 匹配组替换结果。