Reputation: 2653
I want to match an occurrence of 1
that comes after either the beginning of the String or a
.
I've tried [\Aa]1
, but this gives me a PatternSyntaxException.
Upvotes: 0
Views: 125
Reputation: 148990
Try a pattern like this:
(^|a)1
The ^
will match the begining of the string, while the a
will match a literal Latin letter a
. The |
is called an alternation, and will match either the pattern on the left or the right, while parentheses restrict the scope of the alternation.
Now, this will include the a
as part of the matched string. If you'd like to avoid this you can either use a lookbehind, like this:
(?<=^|a)1
This will match a 1
, but only if it is immediately preceeded by the beginning of the string or a Latin letter a
.
Upvotes: 2
Reputation: 124215
I am not sure if that is what you mean but maybe you are looking for something like
(?<=\\A|a)1
or if you are not using Pattern.MULTILINE
flag
(?<=^|a)1
Upvotes: 1