need regular expression that has find two substring or two other substring

I need JUST ONE regular expression to run on C# .NET that will find (ABC | DEF) and (COMPUTER | CONSULTING) all as substrings or no characters or any number of characters around them in any order

anyNumberOrNoCharacters ABC anyNumberOrNoCharacters COMPUTER anyNumberOrNoCharacters
anyNumberOrNoCharacters COMPUTER anyNumberOrNoCharacters ABC anyNumberOrNoCharacters

or

anyNumberOrNoCharacters DEF anyNumberOrNoCharacters CONSULTING anyNumberOrNoCharacters
anyNumberOrNoCharacters CONSULTING anyNumberOrNoCharacters DEF anyNumberOrNoCharacters

I can find just the first with this

(?>ABC|DEF)

but I cannot figure out how to put two of them in one statement and put an and that requires both of them to be found to consider it a match

UPDATE to better describe what I'm looking for

here's what would NOT be considered a match

1) asdf ABC asdf DEF asdf (because it does not have COMPUTER or CONSULTING) 2) asdf COMPUTER asdf CONSULTING asdf (because it does not have ABC or DEF) 3) asdf ABCDEF asdf (because it does not have COMPUTER or CONSULTING) 4) asdfCOMPUTERCONSULTINGasdf (because it does not have ABC or DEF) 5) asdf GHI asdf (because it does not have ABC or DEF and also because it does not have COMPUTER or CONSULTING)

here's would WOULD be considered a match

6) asda ABC asdf COMPUTER asdf 7) asda ABCCOMPUTER 8) asdf COMPUTER asda ABC asdf 9) asdfCOMPUTERABCasdfasdf

10) asda DEF asdf COMPUTER asdf 11) asda DEFCOMPUTER 12) asdf COMPUTER asda DEF asdf 13) asdfCOMPUTERDEFasdfasdf

14) asda DEF asdf CONSULTING asdf 15) asda DEFCONSULTING 16) asdf CONSULTING asda DEF asdf 17) asdfCONSULTINGDEFasdfasdf

18) asda ABC asdf CONSULTING asdf 19) asda ABCCONSULTING 20) asdf CONSULTING asda ABC asdf 21) asdfCONSULTINGABCasdfasdf

so really it requires ANY substring of the two sets of characters but both of the two sets must be found for it to consider anything a match

Also I'm looking for a regular expression in the format that can be used in .net framework c# with that syntax - there seems to be differences in the syntax for perl and javascript compared to .net c#


To assert that both matches are present in your string, try this:

/^(?=.*(?>ABC|DEF))(?=.*(?>COMPUTER|CONSULTING)).+/

Expression explanation:

  • ^ Asserts position at start of string.
  • (?=.*(?>ABC|DEF)) Asserts that "ABC" or "DEF" is present in the string. No backtracking
  • (?=.*(?>COMPUTER|CONSULTING)) Asserts that "COMPUTER" or "CONSULTING" is present in the string. No backtracking
  • .+ Now that our phrases are present, this string is valid. Go ahead and use .+ to match the entire string.
  • Here is a regex demo!

    Read more:

  • Regex for existence of some words whose order doesn't matter
  • Lookahead and Lookbehind Zero-Length Assertions

  • 这是你想要的 ?

    (?:bABCb|bDEFb)|(?:bCOMPUTERb|bCONSULTINGb)
    
    链接地址: http://www.djcxy.com/p/76732.html

    上一篇: 正则表达式的所有合法正则表达式的

    下一篇: 需要找到两个子字符串或两个其他子字符串的正则表达式