gpt4 book ai didi

java正则表达式匹配包含没有数字的单词的字符串,可以选择用逗号分隔

转载 作者:行者123 更新时间:2023-11-30 08:28:14 26 4
gpt4 key购买 nike

受上一个问题的启发,我试图找到一个正则表达式来匹配包含至少一个仅由字符而非数字组成的单词的字符串。所以\w 不适用。仅当连续没有两个逗号时,逗号分隔的单词才可以。

这是我发现的最好的:

(.*\s+,?)*([a-zA-Z]+)+(,?\s+.*)*

但它不匹配以下字符串:

aaaaa,11111
11111,aaaaa
11111,aaaaa,
,aaaaa
aaaaa,
,aaaaa,
aaaaa,11111,,
,,aaaaa,bbbbb
aaaaa,,bbbbb,ccccc
aaaaa,bbbbb,,ccccc
aaaaa,bbbbb,ccccc
aaaaa,11111

这是一个测试程序,用于确定正则表达式是否正确:

import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
String regex = "(.*\\s+,?)*([a-zA-Z]+)+(,?\\s+.*)*";
String shouldMatch[] = new String[] {
"aaaaa",
"aaaaa bbbbb",
"aaaaa 11111",
"11111 aaaaa",
"aaaaa,11111",
"aaaaa, 11111",
"aaaaa, 11111",
"11111,aaaaa",
"11111, aaaaa",
"11111, aaaaa",
"11111,aaaaa,",
",aaaaa",
"aaaaa,",
",aaaaa,",
"aaaaa,11111,,",
",,aaaaa,bbbbb",
"aaaaa1111 bbbbb",
"aaaaa1111 bbbbb ccccc",
"aaaaa1111bbbbb ccccc",
"aaaaa11111bbbbb ccccc 22222",
",,aaaaa bbbbb",
"aaaaa,,bbbbb ccccc",
"aaaaa,,bbbbb,ccccc",
"aaaaa,bbbbb,,ccccc",
"aaaaa,bbbbb,ccccc",
"aaaaa,11111"
};

String shouldNotMatch[] = new String[] {
"aaaaa11111",
"11111bbbbb",
"aaaaa11111bbbbb",
"aaaaa11111bbbbb 11111ccccc",
"aaaaa11111bbbbb ccccc11111",
"aaaaa,,bbbbb",
"aaaaa,,11111",
",,aaaaa",
"aaaaa,,",
"11111",
"11111,22222",
"11111 22222",
""
};

boolean result = true;

for(String stringToTest : shouldMatch){
if (!(stringToTest.matches(regex))){
System.out.println(stringToTest + " Don't match. WRONG.");
result = false;
}
}

for(String stringToTest : shouldNotMatch){
if (stringToTest.matches(regex)){
System.out.println(stringToTest + " Match. WRONG.");
result = false;
}
}

if (result){
System.out.println("Congratulation, your regex is right.");
}
else {
System.out.println("Result of one ore more test is wrong.");
}
}
}

编辑:添加了一些不应与正则表达式、空字符串和数字(​​加上逗号或空格)匹配的更多字符串。

最佳答案

这有效,我检查了你的测试程序:

String regex = "^.*?(?<=\\s|^|,)(?<!,,)[A-Za-z]+(?!,,)(?=\\s|,|$).*$";

Regular expression visualization

^ “开始于”

.*?对任何非换行符都是非贪婪的

(?<=\\s|^|,)正面查看后面的空格或字符串开头或 , ,因为它们是唯一可以出现在我们的单词定义之前的有效字符

(?<!,,) ,,的负向后视,因为它们现在可以出现在 word
之前 [A-Za-z]+ 1 个或多个字母

(?!,,) ,,的负面展望因为现在允许在字后使用它们

(?=\\s|,|$)正面查找空格或字符串结尾或 , ,因为它们是唯一可以出现在我们的单词定义之后的有效字符

$ “结束于”

关于java正则表达式匹配包含没有数字的单词的字符串,可以选择用逗号分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20347448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com