+ Start a Discussion
PapsakPPapsakP 

regex...am i formatting this wrong?

I am trying to get the value 'S848270' out of the following text string... 

 

"Tel: 0712222222
Email: xxxxxxx@gmail.com
Your property: BEREA, 8 Tudhope Heights, Corner Primrose & Tudhope
Street (S848270)
Message: Hi Obotseng Phokompe, I found this listing on Private Property
and would like more information."

 

 

I am trying to use the following reg expression.  'S[0-9][0-9][0-9][0-9][0-9][0-9]'

 

When i test it in a 3rd party reg expression tool it finds the value ok. But in my apex code it is not finding a match for some reason? Do i need to try put other characters into the reg expression because i am using apex? I have no idea why its not working if it appears to work in the 3rd party reg expression matching tool.

 

 

PapsakPPapsakP

 the way my code is....(myPlainText is already set to the long string in my initial post)...if you can come up with a better reg expression... that actually works, that would be great :)

 

 

 static pattern PATTERN_PRIVATEPROPERTY_ENQUIRY_REF = pattern.compile('S[0-9][0-9][0-9][0-9][0-9][0-9]');

 

        mr = PATTERN_PRIVATEPROPERTY_ENQUIRY_REF.matcher(myPlainText);
        if (mr.find()) {
            ref = mr.group(1).deleteWhitespace();
            system.debug('privateproperty reference found:'+ref);

          }

         else

            {

              system.debug('privateproperty reference NOT found:'+ref);

            }

Anoop AsokAnoop Asok

Hi,

Any particular reason to specify 1 as the mr.group() method parameter?

 

 

Thanks,

Anoop

PapsakPPapsakP

no particular reason. Should it be 0? (im not 100% sure on how groups work but im assuming due to the lack of brackets in my reg expression i only have 1 group)

GlynAGlynA

You might want to ditch the regular expression and do something like this:

 

String theResult = 'S' + theString.substringAfter( '(S' ).substringBefore( ')' );

 

If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!

 

-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator

Anoop AsokAnoop Asok

Yes, you only have one group. And since the list index for groups start from 0,  you should be using 0 as the parameter instead of 1. Try running your code in an anonymous block, it'll throw List Index Out Of Bounds exception.

 

Thanks,

Anoop