Not quite sure how to go about this, but basically what I want to do is match a character, say a
for example. In this case all of the following would not contain ma
Basically what I want to do is match any single a that has any other non a character around it (except for the start and end of the string).
^[^\sa]*\Ka(?=[^\sa]*$)
DEMO
\K
discards the previously matched characters and lookahead assertes whether a match is possibel or not. So the above matches only the letter a
which satifies the conditions.
OR
a{2,}(*SKIP)(*F)|a
DEMO
You need two things:
[^a]
(all except "a")^
and $
) to ensure that the limits of the string are reached (in other words, that the pattern matches the whole string and not only a substring):Result:
^[^a]*a[^a]*$
Once you know there is only one "a", you can use the way you want to extract/replace/remove it depending of the language you use.
You may use a combination of a lookbehind and a lookahead:
(?<!a)a(?!a)
See the regex demo and the regex graph:
Details
(?<!a)
- a negative lookbehind that fails the match if, immediately to the left of the current location, there is a a
char a
- an a
char(?!a)
- a negative lookahead that fails the match if, immediately to the right of the current location, there is a a
char.