Regular Expression for Words with spaces

Problem: 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 the below ones. Nothing works

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

How do I check for words with spaces?

Solution:

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

[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).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s