开发者

Matching a string that starts with a specific character

开发者 https://www.devze.com 2023-01-05 21:46 出处:网络
I am trying to get some Regex to match and string that begins with ~ and ends with a space or the end of the line.

I am trying to get some Regex to match and string that begins with ~ and ends with a space or the end of the line.

It's part of a Wiki Converter I'm cobbling together... I need to wrap anything that starts in ~ upto the next space (or EOL) in tags.

Example strings are:

"~Test"          // matches Test
"~----"          // matches ----
"~Test Bob"      // match开发者_运维问答es Test
"~Test, Bob"     // matches Test,
"Some ~Test Bob" // matches Test
"Some ~Test"     // matches Test

Thanks


(?<=~)[^\s]+

Read as "look behind for a tilde, then match anything after that except for whitespace."


(?<=~)\S+

Same as the other answer, but better match for non-whitespace.
Or, simpler,

~(\S+)

If you are using group anyway

0

精彩评论

暂无评论...
验证码 换一张
取 消