1 Replies - 322 Views - Last Post: 04 July 2017 - 07:18 AM Rate Topic: -----

#1 general656  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 287
  • Joined: 25-March 15

Regex - Everything but Digits and Space?

Posted 04 July 2017 - 06:54 AM

  • I'm not sure if this topic is supposed to be in here since I didn't find any Section Regex-Related but the Snippets where I'm supposed to make a snippet and not a question.

But my problem is this. I want to escape anything that isn't a digit or a space. But with some restrictions.
In first implementation:
555 555\r\n555 555 555\r555
 | | | | | | | | | | | | | | | |
 ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ 
{ "555 555", "555 555 555", "555"}


Well, the Regex below has to escape anything that isn't digit or space.
Regex:
([^\d\s])


Well, after this step, I want to create a Regex that will implement this:
555 555\r\n 555 555 555\r555
 | | | | | | | | | | | | | | | |
 ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼
{ "555 555", "555 555 555", "555"}

See the little space after the escape characters on the first string? Well, I would like to escape that as well. :/

How will I do that in Regex?

EDIT:
The post was edited due to the fact that I solved the first one, but the second one is a liitle bit more difficult to be solved by a beginner like me.

This post has been edited by general656: 04 July 2017 - 07:19 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Regex - Everything but Digits and Space?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2511
  • View blog
  • Posts: 3,983
  • Joined: 21-June 11

Re: Regex - Everything but Digits and Space?

Posted 04 July 2017 - 07:18 AM

What exactly do you mean by "escape" here? I was expecting a bunch of backslashes in the output, but instead there was an array, so it looks like you actually want to split the string?

Anyway, to negate a character class, you use the ^ character inside the character class. So [^\d ] matches anything that's not a digit or a space. {1,} can be more conveniently written as +, so we get [^\d ]+.

To also match spaces that follow non-digit non-space characters, you can use [^\d ]+ * or, if you also want to match subsequent non-digit non-space characters in a single match, you can just do [^\d ]\D*. This would match one non-digit non-space character followed by zero or more non-digit characters.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1