Click here to Skip to main content
15,036,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a science teacher who has developed a self-marking homework program. To mark questions requiring a sentence answer I have often used the RegEx below where A, B, C and D are required words in any position or order, and N is a forbidden word. 
/^(?!.*?(N))(?=.*?\b(A))(?=.*?\b(B))(?=.*?\b(C))(?=.*?\b(D)).{0,150}$/i
I need to develop a more sophisticated RegEx which links pairs of words so that the answer will fail if one of the other required words is positioned between the pair.
So if A and B are linked the following would be accepted:
A B C D		B A C D		D C A B		A and B C D	("and" is not a required word, so can be inserted)
But these would not
A C B D		B D A C		etc
Presumably you could also do 2 Pairs, A and B, C and D. A good example of using this would be a chemical equation:
NaOH + HCl -> NaCl + H2O 	where H2O + NaCl <- HCl + NaOH is allowed 
But NaOH + NaCl -> Hcl + H2O is not.


What I have tried:

Insufficient ability to try anything.
Posted
Updated 5-Dec-20 13:06pm

Basically, you have a hammer, and you are trying to use it to screw boards together: it might work in some way, but it's not as easy and reliable as using the right tool - a screwdriver!

Regular Expressions are text matching tools, they are great at spotting substrings and have a little limited counting boult in - but what you are doing is moving into the world of grammars and syntaxes - an while you may be able to cobble something together in a Regex that will work for a single example gettin it to work for all possible inputs is really not feasible as they very quickly become unreadable and unmaintainable.

Instead you should be looking to use a Parser: Parsing in C#: all the tools and libraries you can use[^] will outline the process and Google will help you fill it in.
   
Quote:
I need to develop a more sophisticated RegEx

Bexare, RegEx is not the answer to everything, it is good at matching sequences in strings, but usually, a little piece of code will be simpler for special requirements.
Quote:
Science teacher with almost no regex knowledge needs help!

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
   
You should use "cloud AI". This is like building a sky scraper out of mini-bricks. You can get there but school will be out before you do.

You train AI / ML (Machine Learning) with with various "right and wrong" answers, then let the AI sort it out while you tune it.

You can usually get free test runs and as an educator probably a discount. Good for the CV too. See Microsoft ML, Amazon, Google, etc.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900