2

I want to check for the following strings using regExpression:

1) Main-xxxxx-Accepted 2) Main-xxx xxx-Accepted

I used the below

String regEx = 'Main-[aA-zZ-]*-Accepted';

Pattern.matches(regEx,'Main-SUBMITTED-Accepted')); results in **TRUE**

It works fine for Main-SUBMITTED-Accepted. But it fails for a word with spaces (non submitted)

Pattern.matches(regEx,'Main-NOT SUBMITTED-Accepted'));  results in **FALSE**

I tried these. Nothing works

String regEx = 'Main-[aA-zZ-\\s]*-Accepted';
String regEx = 'Main-[aA-zZ-\s]*-Accepted';

How do I check for words with spaces?

  • [aA-zZ-\s] means "any character that is either an a, or a character in the range A through z (which contains several non-letters, incidentally), or a character in the range Z through \s (which is an illegal range, apparently ignored by Salesforce). – Tim Pietzcker Jul 20 '15 at 9:15
2

Try with this:

String regEx = 'Main-[a-zA-Z0-9- ]*-Accepted';


Copy and Paste Code:

String regEx = 'Main-[a-zA-Z0-9- ]*-Accepted';
System.debug(' @@@@@@ Main-NOT SUBMITTED-Accepted '+Pattern.matches(regEx,'Main-NOT SUBMITTED-Accepted'));
System.debug(' @@@@@@ Main-NOT-SUBMITTED-Accepted '+Pattern.matches(regEx,'Main-NOT-SUBMITTED-Accepted'));
System.debug(' @@@@@@ Main-NOT$-SUBMITTED-Accepted '+Pattern.matches(regEx,'Main-NOT$-SUBMITTED-Accepted'));
System.debug(' @@@@@@ Main45NOT-SUBMITTED-Accepted '+Pattern.matches(regEx,'Main45NOT-SUBMITTED-Accepted'));
  • 1
    Works perfectly. But i wonder why _(underscore) in your answer instead of -(hyphen) mentioned in question! – Vigneshwaran G Jul 20 '15 at 7:53
  • 1
    It is hypen now. Just a typo mistake @VigneshwaranG – Ashwani Jul 20 '15 at 7:58
  • Thanks that helped! perfect. I removed 0-9 since I need to test for text only. Thanks a lot. Cheers! [aA-zZ ]* with space doesn't work though. It had to be [a-Az-Z ]* – sfdcFanBoy Jul 20 '15 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.