tags:

views:

43

answers:

2

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"      // matches Test
"~Test, Bob"     // matches Test,
"Some ~Test Bob" // matches Test
"Some ~Test"     // matches Test

Thanks

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

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

mquander
Thanks, added ()'s to get exclude the tilde from the match.
DaveShaw
+1  A: 
(?<=~)\S+

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

~(\S+)

If you are using group anyway

unbeli