anthonyms
anthonyms

Reputation: 948

regex to match a number followed by spaces

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

Upvotes: 4

Views: 33825

Answers (3)

h2ooooooo
h2ooooooo

Reputation: 39550

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.

Upvotes: 8

gitaarik
gitaarik

Reputation: 46430

Split on:

/\d+ /

Only the first match will be empty because it's the match just before the first number: 1, so you gotta ignore that one.

Upvotes: 1

Babar Al-Amin
Babar Al-Amin

Reputation: 3984

Maybe this will do: [0-9]{1}[\ ]

Upvotes: 3

Related Questions