我需要构建一个正则表达式来匹配这些模式的单词:
字母和数字:
A35,35A,B503X,1ABC 5
由"-“、"/”、“\”分隔的字母和数字:
AB-10,10-AB,A10-BA,BA-A10等.
我为它写了这个正则表达式:
\b[A-Za-z]+(?=[(?<!\-|\\|\/)\d]+)[(?<!\-|\\|\/)\w]+\b|\b[0-9]+(?=[(?<!\-|\\|\/)A-Za-z]+)[(?<!\-|\\|\/)\w]+\b它部分工作,但它只匹配字母或只有数字分开的符号。示例:
10-10,开放式办公室等.
我不想要这个火柴。
我想我的准则是非常重复和有点丑陋。但这是我现在拥有的。
有人能帮我吗?
我正在使用java/groovy。
提前谢谢。
发布于 2011-04-20 15:13:21
有趣的挑战。下面是一个带有正则表达式的java程序,它可以识别您所追求的“单词”的类型:
import java.util.regex.*;
public class TEST {
public static void main(String[] args) {
String s = "A35, 35A, B503X, 1ABC5 " +
"AB-10, 10-AB, A10-BA, BA-A10, etc... " +
"10-10, open-office, etc.";
Pattern regex = Pattern.compile(
"# Match special word having one letter and one digit (min).\n" +
"\\b # Match first word having\n" +
"(?=[-/\\\\A-Za-z]*[0-9]) # at least one number and\n" +
"(?=[-/\\\\0-9]*[A-Za-z]) # at least one letter.\n" +
"[A-Za-z0-9]+ # Match first part of word.\n" +
"(?: # Optional extra word parts\n" +
" [-/\\\\] # separated by -, / or //\n" +
" [A-Za-z0-9]+ # Match extra word part.\n" +
")* # Zero or more extra word parts.\n" +
"\\b # Start and end on a word boundary",
Pattern.COMMENTS);
Matcher regexMatcher = regex.matcher(s);
while (regexMatcher.find()) {
System.out.print(regexMatcher.group() + ", ");
}
}
}以下是正确的输出:
A35, 35A, B503X, 1ABC5, AB-10, 10-AB, A10-BA, BA-A10,
请注意,唯一“丑陋”的复杂正则表达式是那些没有正确格式化和注释的正则表达式!
发布于 2011-04-20 14:33:24
就用这个吧:
([a-zA-Z]+[-\/\\]?[0-9]+|[0-9]+[-\/\\]?[a-zA-Z]+)在Java中,应该转义\\和\/:
([a-zA-Z]+[-\\\/\\\\]?[0-9]+|[0-9]+[-\\\/\\\\]?[a-zA-Z]+)发布于 2011-04-21 00:41:25
请原谅我用Python编写了我的解决方案,我不知道足够的Java来编写Java。
pat = re.compile('(?=(?:([A-Z])|[0-9])' ## This part verifies that
'[^ ]*' ## there are at least one
'(?(1)\d|[A-Z]))' ## letter and one digit.
'('
'(?:(?<=[ ,])[A-Z0-9]|\A[A-Z0-9])' # start of second group
'[A-Z0-9-/\\\\]*'
'[A-Z0-9](?= |\Z|,)' # end of second group
')',
re.IGNORECASE) # this group 2 catches the string。
我的解决方案在第二组中捕获所需的字符串:((?:(?<={ ,])[A-Z0-9]|\A[A-Z0-9])[A-Z0-9-/\\\\]*[A-Z0-9](?= |\Z|,))
。
它之前的部分验证至少有一个字母和至少一个数字存在于所捕获的字符串中:
(?(1)\d|[A-Z])是一个条件正则表达式,意思是“如果组(1)捕捉到某物,那么这里必须有一个数字,否则必须有一个字母”。
组(1)是([A-Z]) in (?=(?:([A-Z])|[0-9])
(?:([A-Z])|[0-9])是一个非捕获组,它匹配一个字母(捕获)或一个数字,所以当它匹配一个字母时,组(1)不是空的。
。
标志re.IGNORECASE允许使用大写字母或小写字母处理字符串。
。
在第二组中,我必须编写(?:(?<=[ ,])[A-Z0-9]|\A[A-Z0-9]),因为不允许具有非固定长度的查找后断言。这个部分表示一个字符不能是'-'前面有一个空白或字符串的头。
相反,(?= |\Z[,)的意思是‘字符串的结尾、逗号的结尾或后面的空白’。
。
这个正则表达式假定字符'-' 、 '/' 、 '\' 不能是捕获的字符串的第一个字符或最后一个字符。对不对?
import re
pat = re.compile('(?=(?:([A-Z])|[0-9])' ## (from here) This part verifies that
'[^ ]*' # there are at least one
'(?(1)\d|[A-Z]))' ## (to here) letter and one digit.
'((?:(?<=[ ,])[A-Z0-9]|\A[A-Z0-9])'
'[A-Z0-9-/\\\\]*'
'[A-Z0-9](?= |\Z|,))',
re.IGNORECASE) # this group 2 catches the string
ch = "ALPHA13 10 ZZ 10-10 U-R open-office ,10B a10 UCS5000 -TR54 code vg4- DV-3000 SEA 300-BR gt4/ui bn\\3K"
print [ mat.group(2) for mat in pat.finditer(ch) ]
s = "A35, 35A, B503X,1ABC5 " +\
"AB-10, 10-AB, A10-BA, BA-A10, etc... " +\
"10-10, open-office, etc."
print [ mat.group(2) for mat in pat.finditer(s) ]结果
['ALPHA13', '10B', 'a10', 'UCS5000', 'DV-3000', '300-BR', 'gt4/ui', 'bn\\3K']
['A35', '35A', 'B503X', '1ABC5', 'AB-10', '10-AB', 'A10-BA', 'BA-A10']https://stackoverflow.com/questions/5732035
复制相似问题