Salviati
Salviati

Reputation: 788

How do I execlude only numbers in this regular expression?

I'm trying to match words that look like this

:test:
:soda_stream:
:test3:

and so on. What I have now is \:\S+\: and it works for everything except I also have parts in the text that have only numbers, like :23:. These I want to exclude, but are unsuccessful in doing that. I took a look at this StackOverflow page, but it does not work.

Do anyone know how to do it? It probably is just some addition that exclude if there is only numbers inside the :'s.

Upvotes: 1

Views: 43

Answers (2)

The fourth bird
The fourth bird

Reputation: 163362

As an alternative, you could match at least a single char that is not a digit or a whitespace char in between the colons:

:[^:\s]*[^:\s\d][^:\s]*:
  • : Match literally
  • [^:\s]* Match 0+ times any char except : or a whitespace char
  • [^:\s\d] Match any char except : or a whitespace char or a digit
  • [^:\s]* Match 0+ times any char except : or a whitespace char
  • : Match literally

Regex demo

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

Taking into account current examples, you may use

:(?!\d+:)[^\s:]+:

See the regex demo

Details

  • : - a colon
  • (?!\d+:) - a negative lookahead that fails the match if there are one or more digits and then : immediately to the right of the current location
  • [^\s:]+ - 1 or more characters other than whitespace and a colon
  • : - a colon

Upvotes: 2

Related Questions