Regex to get digits from a string when there is no separator between digits

Hatem Ahmed Gamil

I have a string like Acc:123-456-789 and another string like -1234567, I need your help to write an expression to match digits in case there is no separator between the digits.

-*(?!\d*(?:\d*-)$)\d*$

Input strings:

Acc:123-456-789   -12323232 7894596

Desired result:

group 1 12323232 
group 2 7894596
John

I think this ought to work:

(?<=^|\s|\s-)(\d+)(?=\s|$)

Breaking it down:

  • (?<=^|\s|\s-) - A positive lookbehind that matches the start of the string, whitespace, or whitespace followed by a -.
  • (\d+) - Matches and captures number sequences.
  • (?=\s|$) - A positive lookahead that matches whitespace or the end of the string.

** Note: If you need to capture negative number sequences, replace (\d+) with (\-?\d+).

Try it online

Regex reference

Remember for use in C# that you need to escape backslashes or use the @ prefix to a string literal (@" ").

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to Extract digits from string?

分類Dev

How do you get the numerical value from a string of digits?

分類Dev

regex ensure string starts and ends with digits

分類Dev

Javascript regex: get all numbers having between 2 and 6 digits but not 5

分類Dev

Excel - Extract all digits from a alphanumeric string

分類Dev

Excel - Extract all digits from a alphanumeric string

分類Dev

Regex to reject sequence of Digits

分類Dev

Regex including unwated digits

分類Dev

RegEx for fixed number of digits?

分類Dev

Regex for digits not repeating more than five times in string such as contact number

分類Dev

Extract part of a string with regex before hyphen followed by digits

分類Dev

Regex check if a string contains only digits doesn't work

分類Dev

String with N digits

分類Dev

How to get sum of all the digits in a string - Visual Basic 2010 (Module)

分類Dev

Regex file name with multiple digits

分類Dev

How to remove digits from the end of a string in Python 3.x?

分類Dev

How to extract digits from a String and transform them into an Integer

分類Dev

How to use dart to remove all but digits from string using RegExp

分類Dev

Dot between every two digits

分類Dev

Regex to get string between \" and \"

分類Dev

C - Get the number of digits in a double

分類Dev

Regex match repeated digits without sorting

分類Dev

Regex to match everything except strings that starts with digits

分類Dev

Regex Match 2 digits or characters side by side

分類Dev

Operation on Regex identified digits in python data frame

分類Dev

Regex to validate phone Number for repeating digits

分類Dev

Regex to allow numbers and digits but only one comma

分類Dev

sed insert spaces between digits for long numbers

分類Dev

unknown space between the digits after echo command

Related 関連記事

  1. 1

    How to Extract digits from string?

  2. 2

    How do you get the numerical value from a string of digits?

  3. 3

    regex ensure string starts and ends with digits

  4. 4

    Javascript regex: get all numbers having between 2 and 6 digits but not 5

  5. 5

    Excel - Extract all digits from a alphanumeric string

  6. 6

    Excel - Extract all digits from a alphanumeric string

  7. 7

    Regex to reject sequence of Digits

  8. 8

    Regex including unwated digits

  9. 9

    RegEx for fixed number of digits?

  10. 10

    Regex for digits not repeating more than five times in string such as contact number

  11. 11

    Extract part of a string with regex before hyphen followed by digits

  12. 12

    Regex check if a string contains only digits doesn't work

  13. 13

    String with N digits

  14. 14

    How to get sum of all the digits in a string - Visual Basic 2010 (Module)

  15. 15

    Regex file name with multiple digits

  16. 16

    How to remove digits from the end of a string in Python 3.x?

  17. 17

    How to extract digits from a String and transform them into an Integer

  18. 18

    How to use dart to remove all but digits from string using RegExp

  19. 19

    Dot between every two digits

  20. 20

    Regex to get string between \" and \"

  21. 21

    C - Get the number of digits in a double

  22. 22

    Regex match repeated digits without sorting

  23. 23

    Regex to match everything except strings that starts with digits

  24. 24

    Regex Match 2 digits or characters side by side

  25. 25

    Operation on Regex identified digits in python data frame

  26. 26

    Regex to validate phone Number for repeating digits

  27. 27

    Regex to allow numbers and digits but only one comma

  28. 28

    sed insert spaces between digits for long numbers

  29. 29

    unknown space between the digits after echo command

ホットタグ

アーカイブ