How to match last 9 digit of number with space flutter

732

Solution 1

You could use repeat matching a digit followed by 0+ times a space 8 times. For the last digit you could also match 0+ times a space and a digit and capture that in a group.

After the captured group match 0* times a space and assert the end of the string.

((?:\d *){8} *\d) *$

Explanation

  • ( Capturing group
    • (?:\d *){8} Repeat 8 times a non capturing group that matches a digit and 0+ times a space
    • *\d match 0+ times a space followed by a digit
  • ) Close non capturing group
  • *$ Match 0+ times a space and assert the end of the string

The 9 digits including whitespace between them are in the first capturing group.

Regex demo

Solution 2

You could use

(?:\+\d{2}\ ?)?(\d[\d ]*\d)

and use the first group, see a demo on regex101.com.

Share:
732
Nitneuq
Author by

Nitneuq

Updated on December 08, 2022

Comments

  • Nitneuq
    Nitneuq over 1 year

    I tried to match only the 9 digits of phone number

    I have multi input

    +33123456789
    

    or

    0123456789
    

    or

    +33 01 23 45 67 89
    

    or

    01 23 45 67 89
    

    I want one output 123456789

    currently I have found how to match +33123456789 or 0123456789

    with (\d{9}$)

    But I don't find how to match with space between

    thank you

  • Nitneuq
    Nitneuq over 5 years
    Sorry i speak too quickly,I search to remove white space in all output like 123456789 even if my input have whitespace
  • The fourth bird
    The fourth bird over 5 years
    One way could be to replace all whitespaces of group 1 with an empty string.