Reputation: 788
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
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 literallyUpvotes: 1
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 colonUpvotes: 2