我正在尝试识别包含现金总额(英镑)参考的字符串,但我需要忽略货币价值为其中的例子:
我想要的一些例子和结果:
Total £500 cash <-- Match
£500 cash <-- Match
Thing not worth much, £1 and some shoes <-- Match
£123 and some shoes <-- Match
Total £1,234 and some shoes <-- Match
Total £2 and some shoes worth not much <-- Match
Total £1000000 and some shoes <-- Match
A gadget worth £89.99 <-- NO match
A £50 shop Voucher <-- NO match
A £50 shop voucher and something else <-- NO match
A £50 voucher and something else <-- NO match
A voucher and £200 cash plus some socks <-- Match
Total £42 cash, a shop Voucher and some cheese <-- Match
我想出的最好的是:
^.*(?<!worth )(?<CashValue>£(\d{1,3})(,?\d{1,3})*)(?!( \w* ?voucher)).*$
这正确地挑选了现金价值,它正确地排除了“价值89.99英镑”的例子。但我无法按照我的意愿将其排除在“凭证”一词之外。 我显然是一个菜鸟并使用前瞻(或后瞻?)错误或错误的东西。
我正在使用Node.js。
答案 0 :(得分:0)
我到了
(?<!worth )(?<CashValue>£(\d{1,3})(,?\d{1,3})*) (?!((\b\w*\b )?voucher))
似乎给出了预期的结果:https://regexr.com/3nqu0
我认为你的正则表达式中的一个问题是你在上一组中没有正确匹配空格。
答案 1 :(得分:0)
在JavaScript中,您需要使用
/(worth\s+)?£(\d[\d.,]*)\b(?!(?:\s+[^\s,]+)*\s*voucher)/i
并检查组1是否匹配。如果是,则应该使匹配失败(如果提取,请不要添加到生成的数组中,如果替换,只需将整个匹配值放回到生成的字符串中)。
<强>详情
(worth\s+)?
- 第1组:可选worth
字符串和1 +空格£
- 一个英镑符号(\d[\d.,]*)
- 第2组:一个数字,然后是0+位,逗号或句号\b
- 字边界(?!(?:\s+[^\s,]+)*\s*voucher)
- 除了空格和,
之外,不能有0 + 1个以上空格的序列,其次是1 +个字符,然后是voucher
JS演示:
var strs = ["Total £500 cash", "£500 cash", "Thing not worth much, £1 and some shoes", "£123 and some shoes", "Total £1,234 and some shoes", "Total £2 and some shoes worth not much", "Total £1000000 and some shoes", "A gadget worth £89.99", "A £50 shop Voucher", "A £50 shop voucher and something else", "A £50 voucher and something else", "A voucher and £200 cash plus some socks", "Total £42 cash, a shop Voucher and some cheese"];
var rx = /(worth\s+)?£(\d[\d.,]*)\b(?!(?:\s+[^\s,]+)*\s*voucher)/i;
for (var s of strs) {
var m;
if (m = s.match(rx)) {
if (m[1]) {
console.log(s, "=> No match");
} else {
console.log(s, "=>", m[2]);
}
} else {
console.log(s, "=> No match");
}
}
&#13;