正则表达式包含所有可选部件,但至少需要一个部件

时间:2021-01-09 11:09:50

I need to write a regex that matches strings like "abc", "ab", "ac", "bc", "a", "b", "c". Order is important and it shouldn't match multiple appearances of the same part.

我需要编写一个匹配字符串的正则表达式,如“abc”,“ab”,“ac”,“bc”,“a”,“b”,“c”。订单很重要,不应该匹配同一部件的多个外观。

a?b?c? almost does the trick. Except it matches empty strings too. Is there any way to prevent it from matching empty strings or maybe a different way to write a regex for the task.

A-B?C?几乎可以解决问题。除了它也匹配空字符串。有没有办法阻止它匹配空字符串或者可能采用不同的方式为任务编写正则表达式。

4 个解决方案

#1


2  

To do this with pure regex you're going to have to expand it into all of its possibilities:

要使用纯正则表达式执行此操作,您将不得不将其扩展为所有可能性:

ab?c?|a?bc?|a?b?c

If you have lookaheads you can make sure the string is non-empty. Or you can verify the string has a length of at least one before passing it to the expression, depending on your choice of language.

如果你有预测,你可以确保字符串是非空的。或者,您可以在将字符串传递给表达式之前验证字符串的长度是否至少为1,具体取决于您选择的语言。

For example a .NET lookahead might look like this:

例如,.NET lookahead可能如下所示:

^(?=[abc])a?b?c?$

Or you could just test your string's length before matching it:

或者您可以在匹配之前测试字符串的长度:

if (YourString.Length == 1) {   // matching code goes here, using the expression a?b?c?}

#2


4  

^(?=.)a?b?c?$

This will check if there is at least one character with lookahead and will match your regex.

这将检查是否至少有一个字符具有前瞻性并且与您的正则表达式匹配。

#3


0  

You can write down all permutations (which is a pain) or all possibilities of which letter is not left out (ab?c?|a?bc?|a?b?c), which is somewhat less of a pain.

你可以记下所有的排列(这是一种痛苦)或者没有遗漏哪些字母的可能性(ab?c?| a?bc?| a?b?c),这有点不那么痛苦。

#4


0  

It is pointless to try to pack all functionality of all problems you ever have into one single regexp. The best solution is to write the obvious regex, and add a check against zero length. You should only get extra clever with regexps when you absolutely have to - for instance if you have to interact with an unchangeable API that accepts only one regexp and nothing more.

尝试将所有问题的所有功能打包到一个正则表达式中是毫无意义的。最好的解决方案是编写明显的正则表达式,并添加一个零长度检查。当你绝对需要时,你应该只使用regexp来获得额外的聪明 - 例如,如果你必须与只接受一个正则表达式的不可更改的API进行交互,而不是更多。

#1


2  

To do this with pure regex you're going to have to expand it into all of its possibilities:

要使用纯正则表达式执行此操作,您将不得不将其扩展为所有可能性:

ab?c?|a?bc?|a?b?c

If you have lookaheads you can make sure the string is non-empty. Or you can verify the string has a length of at least one before passing it to the expression, depending on your choice of language.

如果你有预测,你可以确保字符串是非空的。或者,您可以在将字符串传递给表达式之前验证字符串的长度是否至少为1,具体取决于您选择的语言。

For example a .NET lookahead might look like this:

例如,.NET lookahead可能如下所示:

^(?=[abc])a?b?c?$

Or you could just test your string's length before matching it:

或者您可以在匹配之前测试字符串的长度:

if (YourString.Length == 1) {   // matching code goes here, using the expression a?b?c?}

#2


4  

^(?=.)a?b?c?$

This will check if there is at least one character with lookahead and will match your regex.

这将检查是否至少有一个字符具有前瞻性并且与您的正则表达式匹配。

#3


0  

You can write down all permutations (which is a pain) or all possibilities of which letter is not left out (ab?c?|a?bc?|a?b?c), which is somewhat less of a pain.

你可以记下所有的排列(这是一种痛苦)或者没有遗漏哪些字母的可能性(ab?c?| a?bc?| a?b?c),这有点不那么痛苦。

#4


0  

It is pointless to try to pack all functionality of all problems you ever have into one single regexp. The best solution is to write the obvious regex, and add a check against zero length. You should only get extra clever with regexps when you absolutely have to - for instance if you have to interact with an unchangeable API that accepts only one regexp and nothing more.

尝试将所有问题的所有功能打包到一个正则表达式中是毫无意义的。最好的解决方案是编写明显的正则表达式,并添加一个零长度检查。当你绝对需要时,你应该只使用regexp来获得额外的聪明 - 例如,如果你必须与只接受一个正则表达式的不可更改的API进行交互,而不是更多。