我希望我的正则表达式能够匹配随机字符串的字符串,可选地后跟一些数字 - 但如果两个匹配项都为空,我希望匹配失败。我正在构建正则表达式,如:
public class Testing {
public static final String HD_DATA = "STOCK,D1,D2,D3,D4,D5,D6,D7,D8,D9,D10";
public static final String HD_DATA_1 = "1,10,20,30,40,50,60,70,80,90,100";
public static final String HD_DATA_2 = "2,11,22,33,44,55,66,77,88,99,111";
public static void main(String[] args) {
String[] hdDataArray = HD_DATA.split(",");
String[] hdDataArray1 = HD_DATA_1.split(",");
String[] hdDataArray2 = HD_DATA_2.split(",");
String[][] pram = new String[3][]; //[null, null, null]
pram[0] = hdDataArray;
pram[1] = hdDataArray1;
pram[2] = hdDataArray2;
System.out.println(pram);
}
}
但这也匹配空文件名(仅限扩展名)。无法解决类似的问题:
额外的复杂性是可能不会添加第二组(数字)
如此有效:
regex = u'^(.*)'
if has_digits: regex += u'(\d*)'
regex += ext + u'$' # extension group as in u'(\.exe)'
rePattern = re.compile(regex, re.I | re.U)
如果has_digits为真:
abc%.exe
123.exe
无效:abc 123.exe # I want the second group to contain the 123 not the first one
答案 0 :(得分:2)
正则表达式:
^(.*?)(\d+)?(?<=.)\.exe$
积极的外观确保在扩展部分之前至少有一个字符。
<强> Live demo 强>
集成:
regex = '^(.*?)'
if has_digits: regex += '(\d+)?'
regex += '(?<=.)' + ext + '$'
rePattern = re.compile(regex, re.I | re.U)
答案 1 :(得分:1)
您可以使用这个基于前瞻性的正则表达式:
ext = r'\.exe'
regex = r'^(?=.+\.)(.*?)'
if has_digits: regex += r'(\d*)'
regex += ext + '$'
rePattern = re.compile(regex, re.I | re.U)
# ^(?=.+\.)(.*?)(\d*)\.exe$
Lookahead (?=.+\.)
确保在DOT之前至少存在一个字符。