[英]Regex, only match with strings that contain one occurence of certain characters
I have the following regular expression:我有以下正则表达式:
^[-]?([+]?[\d]+[-+*/]?)+$
My objective is to match strings that contain arithmetic expressions and integers.我的目标是匹配包含算术表达式和整数的字符串。 Which it is successful in, except one case.
它是成功的,除了一个案例。 When it comes to equals signs.
说到等号。 I only want this expression to match strings that contain at most one equals sign.
我只希望这个表达式匹配最多包含一个等号的字符串。 Which would mean that it would match
这意味着它将匹配
7=7
7=7
but not但不是
7=7=7
7=7=7
since the second string has two occurrences of the equals sign.因为第二个字符串出现了两次等号。
I tried using curly braces {} and I think I need something like我尝试使用花括号 {},我想我需要类似的东西
={0,1}
which would match with strings that have exactly one or no occurrencces of "=".这将与恰好出现一次或未出现“=”的字符串匹配。 But I do unfortunately not know how to incorporate it in my regex.
但不幸的是,我不知道如何将它合并到我的正则表达式中。
Since you only want to match arithmetic expressions and integers I changed your code to find everything if it has numbers, operators or a equal sign: ^[-+*\/\d=]+$
.由于您只想匹配算术表达式和整数,因此我更改了代码以查找具有数字、运算符或等号的所有内容:
^[-+*\/\d=]+$
。 I needed to escape the character /
-> \/
我需要转义字符
/
-> \/
To match only one equal sign I addes a negative lookahead: (?..*=.*=)
.为了只匹配一个等号,我添加了一个负前瞻:
(?..*=.*=)
。 If it finds whats insinde the brackets, the whole regex wont match.如果它找到括号内的内容,则整个正则表达式将不匹配。 For example if you have a word mytext:
(?!mytext)
the whole regex won't match.例如,如果您有一个单词 mytext:
(?!mytext)
整个正则表达式将不匹配。 .*
means it finds everything ( .
stands for every character and the *
say's it can be there 0 to unlimited times). .*
表示它可以找到所有内容( .
代表每个字符, *
表示它可以存在 0 次到无限次)。
So this is the solution:所以这是解决方案:
^(?!.*=.*=)[-+*\/\d=]+$
See live example here: https://regex101.com/r/zhSEmf/1/在此处查看实时示例: https://regex101.com/r/zhSEmf/1/
Also your code didn't worked for -4*-4.此外,您的代码不适用于 -4*-4。
Edit: Since you don't want your code to start with / or * I added id in the negative lookahead: ^(?..*=.*=|[\/*].*)[-+*\/\d=]+$
编辑:由于您不希望代码以 / 或 * 开头,因此我在否定前瞻中添加了 id :
^(?..*=.*=|[\/*].*)[-+*\/\d=]+$
If you wish to match strings with only one equals sign then you want to not match strings with no equals and also not match strings with more than one equals.如果你想匹配只有一个等号的字符串,那么你不想匹配没有等号的字符串,也不想匹配多个等号的字符串。 A regex like so:
像这样的正则表达式:
^(?!.*=.*=)(?![^=]*$)[-+*\/\d=]+$
Unfortunately, this will match strings like 9=
.不幸的是,这将匹配像
9=
这样的字符串。 Probably not desired.可能是不希望的。
I suspect then that you want to both match strings of form a=b and strings of form a, where a and b are numbers.我怀疑你想同时匹配 a=b 形式的字符串和 a 形式的字符串,其中 a 和 b 是数字。 Then this will work:
然后这将起作用:
^[-+*\/\d]+(=[-+*\/\d]+){0,1}$
Unfortunately, this will match strings like *9=+8
but fixing this would make the regex much more complex and thus less suitable as an example.不幸的是,这将匹配
*9=+8
之类的字符串,但修复此问题会使正则表达式更加复杂,因此不太适合作为示例。 For example this for an expression without an equals: Regular Expression for Simple Arithmetic String例如,对于没有等号的表达式: Regular Expression for Simple Arithmetic String
See live example here: https://regex101.com/r/AZymgc/1在此处查看实时示例: https://regex101.com/r/AZymgc/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.