Reputation: 2898
For example, for this string I want to match all A
and a
:
"All the apples make good cake."
Here's what I did: /(.)[^.]*\1*/ig
I started by getting the first character in the group, which can be any character: (.)
Then I added [^.]*
because I don't want to match any other character that isn't the first one. Finally I added \1*
because I wanted to match the first character again. All other similar variations that I've tried don't seem to work.
Upvotes: 0
Views: 38
Reputation: 48711
The regex you are trying to build would capture very first character then any thing up to the same character as much as possible, using a negative lookahead (tempered dot):
(?i)(\w)(?:(?!\1).)*
Capturing group 1 holds the character you need. Try it on a live demo.
If regex engine supports \K
match re-setter token then you can append it to the regex above to only match desired part:
(?i)(\w)(?:(?!\1).)*\K
Upvotes: 2