function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LALALALALALA 

regex code problem

Hi, i have a problem about regex, i spend lots of time on it but still didn't solve it. please any one can answer my question.

this is my part of code:
 try{
    String myPlainText= 'Order Number: 2000157 Date Ordered: Wednesday, May 21, 2014 City: Auckland, 2241 State/Province: North Island, New Zealand Phone: 021735562';
  
      Pattern p=Pattern.compile('(?=:)*\\d{7}'); 

      Matcher m = p.matcher(myPlainText);
      if(m.matches())
      {
        system.debug(m.group(1));          
      }      
      // New orderRecord object to be created - set CustomerEmail__c and OrderNumber__c to
      OrderRecord__c record = new OrderRecord__c(CustomerEmail__c = email.fromAddress, CustomerName__c='NINI',
        OrderNumber__c =Integer.valueOf(m.group(1)));

 insert record;  


problems: i use this code to try to extract some data from email, but failed lots of time.
I wanna use regex to extract 2000157 which is order number. but i think my regex are incorrect, so could you help me to fix it? and i can't get value of m.group(1);     
Best Answer chosen by LALALA
CyberJusCyberJus
Change if(m.matches()) to if (m.find()) 

It is not going to match because you arent matching the whole string, just looking for the subgroup. I tested that change and it is working for me.  

All Answers

LALALALALALA
my this code can't running. in my opinion, i think is  Pattern p=Pattern.compile('(?=:)*\\d{7}');  problem. that is why i can't get value from m.group(). is that correct? 

can anyone give me a right code?

thanks thanks@!!!!!
CyberJusCyberJus
Try this for a pattern - 
Pattern p=Pattern.compile('Order Number: ([\d]{7})');
LALALALALALA
@CyberJus

Illegal character sequence '\d' in string literal.
CyberJusCyberJus
Sorry try 
Pattern p=Pattern.compile('Order Number: ([\\d]{7})');

Apex wants double slashes for some reason. 
CyberJusCyberJus
Change if(m.matches()) to if (m.find()) 

It is not going to match because you arent matching the whole string, just looking for the subgroup. I tested that change and it is working for me.  
This was selected as the best answer