user-123
user-123

Reputation: 884

Remove the first single number from a string

I need a way to remove the first single number in a string, if it exists. This number must be followed by a space in order to be removed.

Examples:
"3 8 GB memory card" should be converted to: "8 GB memory card"
"8GB memory card" should stay the same.

Please advise.

Thanks!

Upvotes: 2

Views: 43

Answers (1)

anubhava
anubhava

Reputation: 785186

Use this regex to search:

^(\D*)\d+\s+

And replace it by empty string.

RegEx Demo

This regex matches:

  • ^ - start input
  • \D* - 0 or more non digits
  • \d+ - 1 or more digits
  • \s+ - 1 or more spaces

In Objective C use this regex:

"^([^0-9]*)[0-9]+ +"

Upvotes: 2

Related Questions