+ Start a Discussion
LALALALALALA 

I can't get value of m.group(), please help me!:)

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

      Matcher m = p.matcher(myPlainText);
      if(m.matches())
      {
        system.debug(m.group(1));          
      }      
      
Decimal Ordernum = Decimal.valueOf(m.group(1));
      OrderRecord__c record = new OrderRecord__c(CustomerEmail__c = email.fromAddress, CustomerName__c='NINI',
        OrderNumber__c =Ordernum);

 insert record;  

my qusetion is:
i wanna get a value which is 2000157 from m.group(1). but i can't.
i don't know where have problems in my code, could you tell me and give me a right answer?
i am so appriciate!!
Best Answer chosen by LALALA
SlashApex (Luis Luciani)SlashApex (Luis Luciani)
Hello!

Shingo is correct, find will serve you best. See the code below. I adjusted the regex also so that it matches any length number, just in case you receive a shorter or longer order number.

Good luck!

If this answer helps you on your way, don't forget to select it as the best answer!
 
String myPlainText= 'CompanyName: Heodo Order Number: 2000157 Date Ordered: Wednesday, May 21, 2014 City: Auckland, 2241 State/Province: North Island, New Zealand Phone: 021735562';
  
Pattern p=Pattern.compile('Order Number: \\d+'); 
Matcher m = p.matcher(myPlainText);

if(m.find())
{
    string mtch = m.group(0);
    Decimal orderNumber = Decimal.valueOf(mtch.split(':')[1].trim());    
    OrderRecord__c record = new OrderRecord__c(
        					CustomerEmail__c = email.fromAddress, 
        					CustomerName__c='NINI',
        					OrderNumber__c = orderNumber);

 	insert record;  
}

 

All Answers

Shingo YamazakiShingo Yamazaki
Try to use m.find() instead of m.matches().

Sample:
String myPlainText = 'foo bar baz';

Pattern p = Pattern.compile('bar');
Matcher m = p.matcher(myPlainText);

System.debug(m.matches());  // false
System.debug(m.find());  // true

 
SlashApex (Luis Luciani)SlashApex (Luis Luciani)
Hello!

Shingo is correct, find will serve you best. See the code below. I adjusted the regex also so that it matches any length number, just in case you receive a shorter or longer order number.

Good luck!

If this answer helps you on your way, don't forget to select it as the best answer!
 
String myPlainText= 'CompanyName: Heodo Order Number: 2000157 Date Ordered: Wednesday, May 21, 2014 City: Auckland, 2241 State/Province: North Island, New Zealand Phone: 021735562';
  
Pattern p=Pattern.compile('Order Number: \\d+'); 
Matcher m = p.matcher(myPlainText);

if(m.find())
{
    string mtch = m.group(0);
    Decimal orderNumber = Decimal.valueOf(mtch.split(':')[1].trim());    
    OrderRecord__c record = new OrderRecord__c(
        					CustomerEmail__c = email.fromAddress, 
        					CustomerName__c='NINI',
        					OrderNumber__c = orderNumber);

 	insert record;  
}

 
This was selected as the best answer