0

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#

DkDev
  • 547
  • 5
  • 14
  • 1
    you mean this http://regex101.com/r/lC4eE1/1 ? – Avinash Raj Aug 16 '14 at 13:38
  • to better describe what I'm looking for here's what would NOT be considered a match ABC DEF (because it does not have COMPUTER or CONSULTING) COMPUTER CONSULTING (because it does not have ABC or DEF) GHI – DkDev Aug 17 '14 at 12:38

2 Answers2

1

Is this what you are looking for ?

(?:\bABC\b|\bDEF\b)|(?:\bCOMPUTER\b|\bCONSULTING\b)
hex494D49
  • 9,109
  • 3
  • 38
  • 47
Amer
  • 67
  • 1
  • 12
  • this did not find a match if the string was COMPUTERCONSULTING - sorry if my question was not clear but unihedron's answer worked fine for me – DkDev Aug 17 '14 at 01:35
  • this also considers just ABC a match when i need it to have both ABC and COMPUTER for it to be considered a match at all – DkDev Aug 17 '14 at 01:46
0

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:

Community
  • 1
  • 1
Unihedron
  • 10,902
  • 13
  • 62
  • 72
  • thank you - i adjusted it to be this and it worked like what I was looking for which was allowing the text to include more than just one of these two words and in any order (?=.*(?>ABC|DEF))(?=.*(?>COMPUTER|CONSULTING)) – DkDev Aug 22 '14 at 21:58