Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex to match a number followed by spaces

Tags:

java

regex

I'm looking to use reg-ex to split the following string

1 hi my name is John. 2 I live at house 32. 3 I see stars.

to

[hi my name is John,  I live at house 32. , I see stars]

Note that am trying to split on digit followed by a space

like image 877
anthonyms Avatar asked Dec 21 '22 04:12

anthonyms


1 Answers

Split on /(^|\b\s+)\d+\s+/g.

Explanation:

  • (^|\b\s+) A collection of either ^ or \b\s+)
    • ^ Start of the string OR
    • \b\s+ a word boundary followed by a space/tab repeated 1 or more times
  • \d+ A digit between 0 and 9 repeated 1 or more times (so it'd match 1, 12, 123, etc.)
  • \s+ A space/tab repeated 1 or more times

Edit:

(^|\.\s+)\d+\s+ might work better for you.

like image 95
h2ooooooo Avatar answered Dec 22 '22 17:12

h2ooooooo