351

The following should be matched:

AAA123
ABCDEFGH123
XXXX123

can I do: ".*123" ?

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
Saobi
  • 13,579
  • 25
  • 67
  • 79
  • 3
    This link shows an approach that seems to work --> [^]+ Which means ‘don’t match no characters’, a double negative that can re-read as ‘match any character’. Source - https://loune.net/2011/02/match-any-character-including-new-line-in-javascript-regexp/ – HockeyJ Jul 14 '16 at 10:30

11 Answers11

741

Yes, you can. That should work.

  • . = any char except newline
  • \. = the actual dot character
  • .? = .{0,1} = match any char except newline zero or one times
  • .* = .{0,} = match any char except newline zero or more times
  • .+ = .{1,} = match any char except newline one or more times
PHP Guru
  • 964
  • 4
  • 15
Delan Azabani
  • 73,106
  • 23
  • 158
  • 198
59

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();
BlueRaja - Danny Pflughoeft
  • 75,675
  • 28
  • 177
  • 259
  • 11
    That's some very useful information! I assumed `.` would match newlines. I'm glad I read your answer, I need to use that! – Ben Kane Sep 04 '13 at 14:30
  • 1
    You may also sometimes need to match newlines in Java regexes in contexts where you cannot pass Pattern.DOTALL, such as when doing a multi-line regex search in Eclipse, or as a user of any Java application that offers regex search. Based on [regular-expression.info's guide](http://www.regular-expressions.info/dot.html), you may need to use `{.,\n,\r,\u2028,\u2029,\u0085}` to match absolutely any character (the Unicode characters are additional line-terminating characters added not matched by `.` in Java), but just `{.,\n,\r}` would work for most text files. – Theodore Murdock Nov 03 '15 at 00:16
  • 8
    @TheodoreMurdock `[\s\S]` is a popular way of matching any character if you can't use DOTALL. – mpen Mar 14 '16 at 16:44
  • In case it would come to your mind, do NOT use `(?:.|\\v)*`, because of [JDK-6337993](https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6337993). – Olivier Cailloux Dec 31 '18 at 13:44
31

Use the pattern . to match any character once, .* to match any character zero or more times, .+ to match any character one or more times.

thr
  • 18,022
  • 19
  • 89
  • 129
  • 1
    This is the only one that works in visual studio's Find tool, but it doesn't match newlines :( – MGOwen Jul 15 '20 at 05:36
16

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.

James Davis
  • 588
  • 5
  • 11
  • 2
    For reference, from https://www.regular-expressions.info/dot.html: "JavaScript and VBScript do not have an option to make the dot match line break characters. In those languages, you can use a character class such as [\s\S] to match any character. This character matches a character that is either a whitespace character (including line break characters), or a character that is not a whitespace character. Since all characters are either whitespace or non-whitespace, this character class matches any character." – Dean Or Aug 26 '19 at 21:44
  • Vote up this answer. The accepted answer does not answer the question but this does. – PHP Guru Oct 25 '20 at 17:24
10

There are lots of sophisticated regex testing and development tools, but if you just want a simple test harness in Java, here's one for you to play with:

    String[] tests = {
        "AAA123",
        "ABCDEFGH123",
        "XXXX123",
        "XYZ123ABC",
        "123123",
        "X123",
        "123",
    };
    for (String test : tests) {
        System.out.println(test + " " +test.matches(".+123"));
    }

Now you can easily add new testcases and try new patterns. Have fun exploring regex.

See also

polygenelubricants
  • 348,637
  • 121
  • 546
  • 611
  • 1
    Upvote just for the regular-expressions.info link. Wonderful site for learning regular expressions and for reference. – Freiheit May 26 '10 at 14:19
8

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

Quill
  • 2,690
  • 1
  • 28
  • 40
Huusom
  • 5,116
  • 1
  • 15
  • 15
  • 1
    Upvote here. The OP didn't specify, but it seems correct to add that the pattern will match any character including things like ###123, 123123, %$#123 which the OP may not want. The character class @Huusom uses above will all the OP to use only uppercase alphabetic characters which may have been the intent. – techdude Jan 26 '15 at 22:43
8

.* and .+ are for any chars except for new lines.

Double Escaping

Just in case, you would wanted to include new lines, the following expressions might also work for those languages that double escaping is required such as Java or C++:

[\\s\\S]*
[\\d\\D]*
[\\w\\W]*

for zero or more times, or

[\\s\\S]+
[\\d\\D]+
[\\w\\W]+

for one or more times.

Single Escaping:

Double escaping is not required for some languages such as, C#, PHP, Ruby, PERL, Python, JavaScript:

[\s\S]*
[\d\D]*
[\w\W]*
[\s\S]+
[\d\D]+
[\w\W]+

Test

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class RegularExpression{

    public static void main(String[] args){

        final String regex_1 = "[\\s\\S]*";
        final String regex_2 = "[\\d\\D]*";
        final String regex_3 = "[\\w\\W]*";
        final String string = "AAA123\n\t"
             + "ABCDEFGH123\n\t"
             + "XXXX123\n\t";

        final Pattern pattern_1 = Pattern.compile(regex_1);
        final Pattern pattern_2 = Pattern.compile(regex_2);
        final Pattern pattern_3 = Pattern.compile(regex_3);

        final Matcher matcher_1 = pattern_1.matcher(string);
        final Matcher matcher_2 = pattern_2.matcher(string);
        final Matcher matcher_3 = pattern_3.matcher(string);

        if (matcher_1.find()) {
            System.out.println("Full Match for Expression 1: " + matcher_1.group(0));
        }

        if (matcher_2.find()) {
            System.out.println("Full Match for Expression 2: " + matcher_2.group(0));
        }
        if (matcher_3.find()) {
            System.out.println("Full Match for Expression 3: " + matcher_3.group(0));
        }
    }
}

Output

Full Match for Expression 1: AAA123
    ABCDEFGH123
    XXXX123

Full Match for Expression 2: AAA123
    ABCDEFGH123
    XXXX123

Full Match for Expression 3: AAA123
    ABCDEFGH123
    XXXX123

If you wish to explore the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Emma
  • 1
  • 9
  • 28
  • 53
5

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):

  1. If you are looking for anything including whitespace you can try [\w|\W]{min_char_to_match,}.
  2. If you are trying to match anything except whitespace you can try [\S]{min_char_to_match,}.
Akash Kumar Seth
  • 961
  • 10
  • 15
3

Try the regex .{3,}. This will match all characters except a new line.

Noah H
  • 85
  • 2
  • 9
Ravi Shekhar
  • 2,137
  • 4
  • 16
  • 18
2

[^] 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’.
Anonymous
  • 29
  • 2
-7

I work this Not always dot is means any char. Exception when single line mode. \p{all} should be

String value = "|°¬<>!\"#$%&/()=?'\\¡¿/*-+_@[]^^{}";
String expression = "[a-zA-Z0-9\\p{all}]{0,50}";
if(value.matches(expression)){
    System.out.println("true");
} else {
    System.out.println("false");
}
Noah H
  • 85
  • 2
  • 9