简体   繁体   中英

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..

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

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

^:[^:]+

Try this too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM