Java Regex .* usage - java

.*[A-Z].*
Here A-Z means any letter from Capital A to Z.
What is the purpose of using .* starting and beginning.
Please Explain above regex code

. means match any character in regular expressions. * means zero or more occurrences of the single regex preceding it.

Related

How to reject repetition of character within Java regular expression

I am looking for help with a Java regular expression please.
My regular expression should accept a string of length 5 only, with characters matching [BDILMOP] only.
No repeated characters are allowed - eg. BDILM is allowed, but BDILL or BDLLL are not.
Please help - I'm new to regex and so would appreciate any advice that you could throw my way.
Thanks!
You can use this negative lookahead based regex:
^(?!.*(.).*\1)[BDILMOP]{5}$
(?!.*(.).*\1) is negative lookahead which fails the match if there is any repetition in input. (.) captures a letter in group #1 and \1 is back-reference of the same group thus checking repetition.
RegEx Demo

Can I negate the dot?

The following regular expression matches the character a:
"a"
The following regular expression matches all characters except a:
"[^a]"
The following regular expression matches a ton of characters:
"."
How do I match everything that is not matched by "."? I can't use the same technique as above:
"[^.]"
because inside the brackets, the . changes meaning and only stands for the character . itself :(
The below negative lookahead will work.
(?:(?!.)[\S\s])
Java regex would be,
"(?:(?!.)[\\S\\s])"
DEMO
The idea behind the above regex is, it would match only \r or \n or \t or \f that is the characters which aren't matched by a dot (Multiline mode).
"[^\\.]"
use double backslash for regex used character. for example
\\.\\]\\[\\-\\)\\(\\?

Java regex "[.]" vs "."

I'm trying to use some regex in Java and I came across this when debugging my code.
What's the difference between [.] and .?
I was surprised that .at would match "cat" but [.]at wouldn't.
[.] matches a dot (.) literally, while . matches any character except newline (\n) (unless you use DOTALL mode).
You can also use \. ("\\." if you use java string literal) to literally match dot.
The [ and ] are metacharacters that let you define a character class. Anything enclosed in square brackets is interpreted literally. You can include multiple characters as well:
[.=*&^$] // Matches any single character from the list '.','=','*','&','^','$'
There are two specific things you need to know about the [...] syntax:
The ^ symbol at the beginning of the group has a special meaning: it inverts what's matched by the group. For example, [^.] matches any character except a dot .
Dash - in between two characters means any code point between the two. For example, [A-Z] matches any single uppercase letter. You can use dash multiple times - for example, [A-Za-z0-9] means "any single upper- or lower-case letter or a digit".
The two constructs above (^ and -) are common to nearly all regex engines; some engines (such as Java's) define additional syntax specific only to these engines.
regular-expression constructs
. => Any character (may or may not match line terminators)
and to match the dot . use the following
[.] => it will matches a dot
\\. => it will matches a dot
NOTE: The character classes in Java regular expression is defined using the square brackets "[ ]", this subexpression matches a single character from the specified or, set of possible characters.
Example : In string address replaces every "." with "[.]"
public static void main(String[] args) {
String address = "1.1.1.1";
System.out.println(address.replaceAll("[.]","[.]"));
}
if anything is missed please add :)

What is the responsibility of (.*) in the Java String?

What is the responsibility of (.*) in the third line and how it works?
String Str = new String("Welcome to Tutorialspoint.com");
System.out.print("Return Value :" );
System.out.println(Str.matches("(.*)Tutorials(.*)"));
.matches() is a call to parse Str using the regex provided.
Regex, or Regular Expressions, are a way of parsing strings into groups. In the example provided, this matches any string which contains the word "Tutorials". (.*) simply means "a group of zero or more of any character".
This page is a good regex reference (for very basic syntax and examples).
Your expression matches any word prefixed and suffixed by any character of word Tutorial. .* means occurrence of any character any number of times including zero times.
The . represents regular expression meta-character which means any character.
The * is a regular expression quantifier, which means 0 or more occurrences of the expression character it was associated with.
matches takes regular expression string as parameter and (.*) means capture any character zero or more times greedily
.* means a group of zero or more of any character
In Regex:
.
Wildcard: Matches any single character except \n
for example pattern a.e matches ave in nave and ate in water
*
Matches the previous element zero or more times
for example pattern \d*\.\d matches .0, 19.9, 219.9
There is no reason to put parentheses around the .*, nor is there a reason to instantiate a String if you've already got a literal String. But worse is the fact that the matches() method is out of place here.
What it does is greedily matching any character from the start to the end of a String. Then it backtracks until it finds "Tutorials", after which it will again match any characters (except newlines).
It's better and more clear to use the find method. The find method simply finds the first "Tutorials" within the String, and you can remove the "(.*)" parts from the pattern.
As a one liner for convenience:
System.out.printf("Return value : %b%n", Pattern.compile("Tutorials").matcher("Welcome to Tutorialspoint.com").find());

Require Help for Regular Expression

I am Doing a Check on the JTextfield Values that it Should be XX.YY.Z format
10.01.5
No space at beginning or after allowed.
EDIT:-
How Can I Specify Last as Alphanumeric character i.e. Z can be Number or character
\d matches a digit, and \. matches a dot.
\d\d\.\d\d\.\d
i.e. "\\d\\d\\.\\d\\d\\.\\d".
I don't have much experience in Java but this what I would do in PHP.
^\d\d.\d\d.\d$
\d represents one degit, \d\d represents two degits
^ a caret character is there to ensure that it must start with the number (No spaces at the beginning)
$ a dollar sign ensures that there will be no spaces or other characters at the end.
You could use quantifiers
\d{2}\.\d{2}\.\d
That is the indicated, and your regex becomes more easy to read and to change.
more on Quantifiers

Categories

Resources