The following should be matched:
AAA123
ABCDEFGH123
XXXX123
can I do: \".*123\"
?
[^]
should match any character, including newline. [^
CHARS]
matches all characters except for those in CHARS. If CHARS is empty, it matches all characters.
JavaScript example:
/a[^]*Z/.test("abcxyz \0\r\n\t012789ABCXYZ") // Returns ‘true’.
The most common way I have seen to encode this is with a character class whose members form a partition of the set of all possible characters.
Usually people write that as [\s\S]
(whitespace or non-whitespace), though [\w\W]
, [\d\D]
, etc. would all work.
No, *
will match zero-or-more characters. You should use +
, which matches one-or-more instead.
This expression might work better for you: [A-Z]+123
Yes that will work, though note that .
will not match newlines unless you pass the DOTALL flag when compiling the expression:
Pattern pattern = Pattern.compile(".*123", Pattern.DOTALL);
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.matches();
Specific Solution to the example problem:-
Try [A-Z]*123$
will match 123
, AAA123
, ASDFRRF123
. In case you need at least a character before 123
use [A-Z]+123$
.
General Solution to the question (How to match "any character" in the regular expression):
[\w|\W]{min_char_to_match,}
.[\S]{min_char_to_match,}
.