Reputation: 17051
The following should be matched:
AAA123
ABCDEFGH123
XXXX123
can I do: ".*123"
?
Upvotes: 529
Views: 1737046
Reputation: 45
Just for reference, regular expressions may behave differently in different languages and different modes.
Absolute anything: [\s\S]
. By joining a set with its complement set, you get a universe set.
Literal anything: the dot .
, which usually matches any character but the invisible, such as spaces, tabs and returns. Of course, somehow it can match absolute anything too.
Append *
, +
, {n,}
, {m,n}
(and ?
when necessary) to your expression to determine how many characters you want it to match.
Upvotes: 0
Reputation: 127
I like the following:
[!-~]
This matches all char codes including special characters and the normal A-Z, a-z, 0-9
https://www.w3schools.com/charsets/ref_html_ascii.asp
E.g. faker.internet.password(20, false, /[!-~]/)
Will generate a password like this: 0+>8*nZ\\*-mB7Ybbx,b>
Upvotes: 1
Reputation: 81492
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 timesUpvotes: 1032
Reputation: 2935
Try the regex .{3,}
. This will match all characters except a new line.
Upvotes: 6
Reputation: 27
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");
}
Upvotes: -5
Reputation: 27763
.*
and.+
are for any chars except for new lines.
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.
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]+
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));
}
}
}
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.
jex.im visualizes regular expressions:
Upvotes: 19
Reputation: 107
[^]
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’.
Upvotes: 3
Reputation: 1701
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,}
.Upvotes: 5
Reputation: 978
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.
Upvotes: 43
Reputation: 5912
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
Upvotes: 9
Reputation: 86146
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();
Upvotes: 71
Reputation: 384016
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.
Upvotes: 11
Reputation: 19476
Use the pattern .
to match any character once, .*
to match any character zero or more times, .+
to match any character one or more times.
Upvotes: 61