1

I need a regex to match a string that

  1. either starts with a special character like [#.>+~] and is followed by a lowercase ASCII word, or that
  2. 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
4
  • So you want to match match1match2 if 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. Commented May 27, 2013 at 13:31
  • To make sure /^([#\.>+~]?)([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) Commented May 27, 2013 at 13:31
  • Sorry, I don't get it. The comment field is unsuitable for posting code. Please edit your question instead. Commented May 27, 2013 at 13:32
  • @Tim Pietzcker see edit Commented May 27, 2013 at 13:41

1 Answer 1

1

This regex should do what you need:

/^([#~.>+](?=[a-z]+$)|[*](?=$))([a-z]*)$/

See it on regex101.com

Explanation:

^          # Start of string
(          # Match and capture in group number 1:
 [#~.>+]   # Either: one "special character"
 (?=       #  but only if it's followed by
  [a-z]+   #   at least one lowercase ASCII letter
  $        #   and the end of the string.
 )         #  End of lookahead
|          # OR
 [*]       #  one (different) special character
 (?=$)     #  but only if the string ends right after it.
)          # End of the first capturing group
(          # Match and capture in group number 2:
 [a-z]*    # Zero or more ASCII lowercase letters
)          # End of the second capturing group
$          # End of string
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.