isuru arunoda
isuru arunoda

Reputation: 11

Regex to find words starting with a colon

I'm designing a text editor with vs 2013 express.I want to find words that starting with a colon (:). Currently i use a regex for it.But it's not perfect. This is my regex - ":\w+" It finds words,but quite not the way i want. My objective is to find words starting with a colon.(first character of the word is a colon ex- :Test ) But the words only at the start of a line.(not from the middle of a sentence) And it should exclude words starting with two colons (::Test) Hope you guys could help me..

Upvotes: 0

Views: 1144

Answers (2)

depsai
depsai

Reputation: 415

Whether any colon come in between in the line. or else you can use below code.

^:[^:]+

Try this too.

Upvotes: 0

Avinash Raj
Avinash Raj

Reputation: 174776

Use ^ anchor to match the start of a line.

^:(?!:)\S+

:(?!:) This negative lookahead asserts that the : would be followed by any but not of a :

OR

^:\w+

This matches the : and the following word characters only if it is at the start of a line.

DEMO

Upvotes: 2

Related Questions