Reputation:
I'm taking a beating to build a regular expression.
Rules for matching:
The string to match the RegEx can have the following formats, with the expected respective answers:
public void testRegexToMatchContextToIgnoreFromString() {
String regex = "\\([^\\(].?[EN+].?\\)";
assertTrue("!(EN)CLIENT".matches(regex));
assertTrue("!(EN,PR)CLIENT".matches(regex));
assertTrue("!(PR,EN)CLIENT".matches(regex));
assertFalse("!(PR)CLIENT".matches(regex));
assertFalse("!(CO,PR)CLIENT".matches(regex));
}
I tried various ways, but I understand little on RegEx, so I ended up going in circles not getting anywhere. Can someone help me?
Upvotes: 2
Views: 2464
Reputation: 1830
Here's your regex DEMO
!\([^\(\)]*EN[^\(\)]*\)
NOTE: is not scaped for java.
Upvotes: 0
Reputation: 18552
This should match your requirement (I've tested it with you test cases):
String regex = ".*!\\(.*EN.*\\).*";
Explanation: First zero or more of any character (.*). Then '!(', then zero or more of any character, then 'EN', then zero or more of any character, then the closing parenthesis and again zero or more of any character.
Upvotes: 2