简体   繁体   中英

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

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.

也许这样做: [0-9]{1}[\\ ]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM