replaceAll and replaceAllMapped not working properly flutter

525

Your problem is finding the right match boundaries.

You already understand ^ (start of string) and $ (end of string) are not working for you.

That means, the real solution will depend on what kind of source texts you have.

If the texts are just tokenized texts, with tokens separated with whitespace, you may probably get away with

r"(?:(?:\B\+|\b0)9|\b)[0-9]{11,}|\S+@\S+\.\S+"

See the regex demo.

Another common pattern for in-text email matching is a kind of \w[\w.-]*@[\w.-]+\.\w+:

r"(?:(?:\B\+|\b0)9|\b)[0-9]{11,}|\w[\w.-]*@[\w.-]+\.\w+"

See another regex demo.

The phone number regex matches

  • (?:(?:\B\+|\b0)9|\b) - either + not preceded with a word char, or a 0 not preceded with a word char, and then a 9, or a word boundary position
  • [0-9]{11,} - eleven or more digits.

The email regexps match

  • \S+@\S+\.\S+ - one or more non-whitespace chars, @, one or more non-whitespace, a dot, and again one or more non-whitespaces
  • \w[\w.-]*@[\w.-]+\.\w+ - a word char, zero or more word, . or - chars, a @ char, one or more word, . or - chars, a dot, and finally one or more word chars.
Share:
525
Brightcode
Author by

Brightcode

Updated on December 29, 2022

Comments

  • Brightcode
    Brightcode over 1 year

    I wrote a piece of code where am able to replaced a string with something else using replaceAll and replaceAllMapped

    //this is to replace a number
    String cutNumber(String text) => text.replaceAllMapped(RegExp(r'(^(?:[+0]9)?[0-9]{11,}$)'),
          (Match m) => "<Not Allowed>");
    
    
    //this is to replace an email address first method
    String cutText(String text) => text.replaceAllMapped(
          RegExp(
            r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$',
          ),
          (Match m) => "<Not Allowed>");
    
    
    //this is to replace an email address second method
     String cutText(String text) => text.replaceAll(
          RegExp(
            r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$',
          ),
          "<Not Alloed>");
    

    This usually works if is the single in this format

    String email = "[email protected]"; 
    String number = "089462453746";
    String bothString = "[email protected] and 089462453746";
    
    print cutNumber(number); //prints out <Not Allowed>
    print cutText(email); // both first and second method prints out <Not Allowed>
    print cutNumber(cutText(bothString)); // now this doesn't even replace anything, just prints out [email protected] and 089462453746
    

    for email it outputs <Not Allowed> same with the number but for the bothString it doesn't change anything at all it just prints out [email protected] and 089462453746 instead of <Not Allowed> and <Not Allowed>. Please is there any place am doing something wrong. I want it to replace it even if there are more words in the string

    • Wiktor Stribiżew
      Wiktor Stribiżew about 3 years
      It is fairly easy to deal with the phone number, but email matching is truly hard, what kind of strings those are you are replacing in? Plain text, whitespace-separated tokens?
    • Brightcode
      Brightcode about 3 years
      @WiktorStribiżew are you asking about the regex or replacing string <Not Allowed>
    • Brightcode
      Brightcode about 3 years
      @WiktorStribiżew are you asking about the regex or replacing string <Not Allowed>
    • Wiktor Stribiżew
      Wiktor Stribiżew about 3 years
      Your problem is finding the right match boundaries. You already understand ^ (start of string) and $ (end of string) are not working for you. What will? If the texts are just tokenized texts, you may probably get away with r"(?:(?:\B\+|\b0)9|\b)[0-9]{11,}|\S+@\S+\.\S+", see demo.
    • Brightcode
      Brightcode about 3 years
      Really thank you very much And can you put it up as an answer let me mark it