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?

1
  • [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). Commented Jul 20, 2015 at 9:15

1 Answer 1

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'));
3
  • 1
    Works perfectly. But i wonder why _(underscore) in your answer instead of -(hyphen) mentioned in question! Commented Jul 20, 2015 at 7:53
  • 1
    It is hypen now. Just a typo mistake @VigneshwaranG
    – Ashwani
    Commented Jul 20, 2015 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 ]* Commented Jul 20, 2015 at 8:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .