Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing numbers and separators from a numbered list

Tags:

regex

I am often dealing with list that users submit to a web site. A list usually looks like:

  1. Item
  2. Item

The pattern is usually a number followed by the separator (it can be "-" or "\" or "." or any other typical separators). There can be one or more spaces between the number and separator and between the separator and the list item. Sometimes there is no number in front of the list item and in that case nothing needs to be done. Sometimes there is a number but no separator.

Is there a way to take out the number and/or the separator all together using regular expression?

like image 325
Thomas Avatar asked Oct 19 '25 11:10

Thomas


1 Answers

This will match numbers and separators and the beginning of a line:

^\d+\s*[-\\.)]?\s+

Use it to replace it with an empty strings (depends on the language you are using).

You might have to add more characters to the character class, to match possible separators.

Good source to learn regular expressions: http://www.regular-expressions.info/

like image 96
Felix Kling Avatar answered Oct 21 '25 04:10

Felix Kling