I need a regex to match a string that
- either starts with a special character like
[#.>+~]and is followed by a lowercase ASCII word, or that - consists only of a different special character like
*.
The special character should be captured in group number 1, the following word (or, in the second case, the empty string) in group number 2.
I can handle the first case with /^([#\.>+~]?)([a-z]+)$/, but how can I get the second case into this regex to achieve the following results:
"#word" -> 1 => "#", 2 => "word"
"~word" -> 1 => "~", 2 => "word"
"##word" -> no match
"+#word" -> no match
"!word" -> no match
"*word" -> no match
"word" -> 1 => "", 2 => "word"
"*" -> 1 => "*", 2 => ""
"**" -> no match
"*word" -> no match
match1match2if it occurs at the start of the string or at the end of the string? I suspect you've given us a simplified regex than the one you're actually using. Could you post that instead? Perhaps there's a better way to achieve what you want./^([#\.>+~]?)([a-z]+)$/matches both "+word", "word" and things like single special characters (other than those specified in the first match1 group), which I would put in the second match1, such as "*". Results: ("+word" -> 1 => "+", 2 => "word"), ("word" -> 1 => "", 2 => "word"), ("*" -> 1 => "*", 2 => null)