Regex not matching

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I am using this regex expression:



String regex = '(\d11|\d5 \d6|\+\d12)';

Pattern compiledPattern = Pattern.compile(regex);

Matcher match = compiledPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');

if (match.matches())
System.debug('MATCH');
String phone = match.group(1);
else
System.debug('MISS');



To extract the phone number from this String:




Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)




When I test the Regex in:



  • Java Regular Expression Tester

  • regex101

It works fine.



But when I try and use it in Apex code, it does not find a match.



  1. What am I doing wrong?

  2. How can I fix it?









share|improve this question























  • There is a difference between matches() and find() in apex. Mind adding your apex that isn't working?
    – Derek F
    17 mins ago











  • @DerekF code added
    – Robs
    15 mins ago
















up vote
1
down vote

favorite












I am using this regex expression:



String regex = '(\d11|\d5 \d6|\+\d12)';

Pattern compiledPattern = Pattern.compile(regex);

Matcher match = compiledPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');

if (match.matches())
System.debug('MATCH');
String phone = match.group(1);
else
System.debug('MISS');



To extract the phone number from this String:




Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)




When I test the Regex in:



  • Java Regular Expression Tester

  • regex101

It works fine.



But when I try and use it in Apex code, it does not find a match.



  1. What am I doing wrong?

  2. How can I fix it?









share|improve this question























  • There is a difference between matches() and find() in apex. Mind adding your apex that isn't working?
    – Derek F
    17 mins ago











  • @DerekF code added
    – Robs
    15 mins ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am using this regex expression:



String regex = '(\d11|\d5 \d6|\+\d12)';

Pattern compiledPattern = Pattern.compile(regex);

Matcher match = compiledPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');

if (match.matches())
System.debug('MATCH');
String phone = match.group(1);
else
System.debug('MISS');



To extract the phone number from this String:




Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)




When I test the Regex in:



  • Java Regular Expression Tester

  • regex101

It works fine.



But when I try and use it in Apex code, it does not find a match.



  1. What am I doing wrong?

  2. How can I fix it?









share|improve this question















I am using this regex expression:



String regex = '(\d11|\d5 \d6|\+\d12)';

Pattern compiledPattern = Pattern.compile(regex);

Matcher match = compiledPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');

if (match.matches())
System.debug('MATCH');
String phone = match.group(1);
else
System.debug('MISS');



To extract the phone number from this String:




Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)




When I test the Regex in:



  • Java Regular Expression Tester

  • regex101

It works fine.



But when I try and use it in Apex code, it does not find a match.



  1. What am I doing wrong?

  2. How can I fix it?






apex regular-expressions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 15 mins ago

























asked 24 mins ago









Robs

1,347524




1,347524











  • There is a difference between matches() and find() in apex. Mind adding your apex that isn't working?
    – Derek F
    17 mins ago











  • @DerekF code added
    – Robs
    15 mins ago
















  • There is a difference between matches() and find() in apex. Mind adding your apex that isn't working?
    – Derek F
    17 mins ago











  • @DerekF code added
    – Robs
    15 mins ago















There is a difference between matches() and find() in apex. Mind adding your apex that isn't working?
– Derek F
17 mins ago





There is a difference between matches() and find() in apex. Mind adding your apex that isn't working?
– Derek F
17 mins ago













@DerekF code added
– Robs
15 mins ago




@DerekF code added
– Robs
15 mins ago










2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










The documentation on the Matcher class should shed some light here.



The description for matches() states the following




Attempts to match the entire region against the pattern.




Meanwhile the description for find() is




Attempts to find the next subsequence of the input sequence that matches the pattern. This method returns true if a subsequence of the input sequence matches this Matcher object's pattern.




find() is what you want to use here, since you want to match a portion of the input.



An example for illustration



String regex = '(\d11|\d5 \d6|\+\d12)';
Pattern msgPattern = Pattern.compile(regex);
Matcher m1 = msgPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');

system.debug(m1.matches()); // displays false
system.debug(m1.find()); // displays true





share|improve this answer



























    up vote
    2
    down vote













    It does work - but only if you use the method that yields a substring match.



    String input = 'Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)';
    String regex = '(\d11|\d5 \d6|\+\d12)';

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(input);

    System.debug('Find: ' + m.find()); // true
    System.debug('Group: ' + m.group()); // 07970123123
    System.debug('Matches: ' + m.matches()); // false


    Matcher.matches() asks to find a match against the entire input region. Matcher.find() locates a group within the input region.






    share|improve this answer




















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "459"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f236529%2fregex-not-matching%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      The documentation on the Matcher class should shed some light here.



      The description for matches() states the following




      Attempts to match the entire region against the pattern.




      Meanwhile the description for find() is




      Attempts to find the next subsequence of the input sequence that matches the pattern. This method returns true if a subsequence of the input sequence matches this Matcher object's pattern.




      find() is what you want to use here, since you want to match a portion of the input.



      An example for illustration



      String regex = '(\d11|\d5 \d6|\+\d12)';
      Pattern msgPattern = Pattern.compile(regex);
      Matcher m1 = msgPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');

      system.debug(m1.matches()); // displays false
      system.debug(m1.find()); // displays true





      share|improve this answer
























        up vote
        3
        down vote



        accepted










        The documentation on the Matcher class should shed some light here.



        The description for matches() states the following




        Attempts to match the entire region against the pattern.




        Meanwhile the description for find() is




        Attempts to find the next subsequence of the input sequence that matches the pattern. This method returns true if a subsequence of the input sequence matches this Matcher object's pattern.




        find() is what you want to use here, since you want to match a portion of the input.



        An example for illustration



        String regex = '(\d11|\d5 \d6|\+\d12)';
        Pattern msgPattern = Pattern.compile(regex);
        Matcher m1 = msgPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');

        system.debug(m1.matches()); // displays false
        system.debug(m1.find()); // displays true





        share|improve this answer






















          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          The documentation on the Matcher class should shed some light here.



          The description for matches() states the following




          Attempts to match the entire region against the pattern.




          Meanwhile the description for find() is




          Attempts to find the next subsequence of the input sequence that matches the pattern. This method returns true if a subsequence of the input sequence matches this Matcher object's pattern.




          find() is what you want to use here, since you want to match a portion of the input.



          An example for illustration



          String regex = '(\d11|\d5 \d6|\+\d12)';
          Pattern msgPattern = Pattern.compile(regex);
          Matcher m1 = msgPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');

          system.debug(m1.matches()); // displays false
          system.debug(m1.find()); // displays true





          share|improve this answer












          The documentation on the Matcher class should shed some light here.



          The description for matches() states the following




          Attempts to match the entire region against the pattern.




          Meanwhile the description for find() is




          Attempts to find the next subsequence of the input sequence that matches the pattern. This method returns true if a subsequence of the input sequence matches this Matcher object's pattern.




          find() is what you want to use here, since you want to match a portion of the input.



          An example for illustration



          String regex = '(\d11|\d5 \d6|\+\d12)';
          Pattern msgPattern = Pattern.compile(regex);
          Matcher m1 = msgPattern.matcher('Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)');

          system.debug(m1.matches()); // displays false
          system.debug(m1.find()); // displays true






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 11 mins ago









          Derek F

          18.2k31646




          18.2k31646






















              up vote
              2
              down vote













              It does work - but only if you use the method that yields a substring match.



              String input = 'Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)';
              String regex = '(\d11|\d5 \d6|\+\d12)';

              Pattern p = Pattern.compile(regex);
              Matcher m = p.matcher(input);

              System.debug('Find: ' + m.find()); // true
              System.debug('Group: ' + m.group()); // 07970123123
              System.debug('Matches: ' + m.matches()); // false


              Matcher.matches() asks to find a match against the entire input region. Matcher.find() locates a group within the input region.






              share|improve this answer
























                up vote
                2
                down vote













                It does work - but only if you use the method that yields a substring match.



                String input = 'Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)';
                String regex = '(\d11|\d5 \d6|\+\d12)';

                Pattern p = Pattern.compile(regex);
                Matcher m = p.matcher(input);

                System.debug('Find: ' + m.find()); // true
                System.debug('Group: ' + m.group()); // 07970123123
                System.debug('Matches: ' + m.matches()); // false


                Matcher.matches() asks to find a match against the entire input region. Matcher.find() locates a group within the input region.






                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  It does work - but only if you use the method that yields a substring match.



                  String input = 'Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)';
                  String regex = '(\d11|\d5 \d6|\+\d12)';

                  Pattern p = Pattern.compile(regex);
                  Matcher m = p.matcher(input);

                  System.debug('Find: ' + m.find()); // true
                  System.debug('Group: ' + m.group()); // 07970123123
                  System.debug('Matches: ' + m.matches()); // false


                  Matcher.matches() asks to find a match against the entire input region. Matcher.find() locates a group within the input region.






                  share|improve this answer












                  It does work - but only if you use the method that yields a substring match.



                  String input = 'Inbound Call at 5:10:54 PM from 07970123123 (Jack Daniels)';
                  String regex = '(\d11|\d5 \d6|\+\d12)';

                  Pattern p = Pattern.compile(regex);
                  Matcher m = p.matcher(input);

                  System.debug('Find: ' + m.find()); // true
                  System.debug('Group: ' + m.group()); // 07970123123
                  System.debug('Matches: ' + m.matches()); // false


                  Matcher.matches() asks to find a match against the entire input region. Matcher.find() locates a group within the input region.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 12 mins ago









                  David Reed

                  22.3k31640




                  22.3k31640



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f236529%2fregex-not-matching%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      Installing NextGIS Connect into QGIS 3?

                      One-line joke