How can I set the string length of this regular expression? - java

I am trying to build a regular expression with this criteria:
Not start or end with these characters '-', '.', '_'
The string must be between 4 to 8 length
Example of allowed strings:
hello
hellowor
he_ll_ow
he-ll.ow
Not allowed strings:
-hello
_hello
.hello
hello_
toolongstringhere
t
Currently, I have this expression:
Pattern.compile("^[^-_\\.](.*[^-_\\.])?$");
This is working for the allowed characters but when I tried to add the length the expression does not work anymore for the rule of the not allowed expressions at the end.

After matching the first and last character, the length of the string in between must be 2-6 characters.
^[^._-].{2,6}[^._-]$

Use this regex:
^[^-.、_][\\w-.、_]{2,6}[^-.、_]$

Related

mongo query that has regex returns null for string that contains special character as ^ [duplicate]

I am trying to find the following text in my string : '***'
the thing is that the C# Regex mechanism doesnt allow me to do the following:
new Regex("***", RegexOptions.CultureInvariant | RegexOptions.Compiled);
due to
ArgumentException: "parsing "*" - Quantifier {x,y} following nothing."
obviously it thinks that my stars represents regular expressions,
is there a way to tell the Regex mechanism to treat stars as just stars and nothing else?
* in Regex means:
Matches the previous element zero or more times.
so that, you need to use \* or [*] instead.
explain:
\
When followed by a character that is not recognized as an escaped character in this and other tables in this topic, matches that character. For example, \* is the same as \x2A.
[ character_group ]
Matches any single character in character_group.
You need to escape the star with a backslash: #"\*"

Java-flavoured Regex : Match whole string if group is n chars

I'm trying to create a Regex for a String validator. My String must be exactly 8 characters long, and begin with a letter (lowercase or uppercase) or a number. It can only contain letters (lowercase and uppercase), numbers or whitespaces right after that first character. If a whitespace is found, there can only be whitespaces after it.
For now, I have the match group for the second part : [a-zA-Z0-9]{1,}\s*
I can't find a way to specify that this group is matched only if it has exactly 8 characters. I tried ^([a-zA-Z0-9]{1,}\s*){8}$ but this is not the expected result.
Here are some test cases (with trailing whitespaces).
Valid :
9013
20130
89B
A5000000
Invalid :
9013
20130
90 90
123456789
There probably is a smart regex way to do it but you could also first check the length of the string:
input.length() == 8 && input.matches("[a-zA-Z0-9]+\\s*")
This is also probably more efficient than a complex regex.
You can use this lookahead based regex:
^[a-zA-Z0-9](?!.* [a-zA-Z0-9])[a-zA-Z0-9 ]{7}$
RegEx Demo
^[a-zA-Z0-9] matches an alpha-num char at start
(?!.* [a-zA-Z0-9]) is negative lookahead to make sure that there is no instance of an alpha-num char followed by a space.
[a-zA-Z0-9 ]{7}$ matches 7 chars containing alpha-num char or space.

What is the meaning of [...] regex?

I am new to regex going through the tutorial I found the regex [...] says Matches any single character in brackets.. So I tried
System.out.println(Pattern.matches("[...]","[l]"));
I also tried escaping brackets
System.out.println(Pattern.matches("[...]","\\[l\\]"));
But it gives me false I expected true because l is inside brackets.
It would be helpful if anybody clear my doubts.
Characters that are inside [ and ] (called a character class) are treated as a set of characters to choose from, except leading ^ which negates the result and - which means range (if it's between two characters). Examples:
[-123] matches -, 1, 2 or 3
[1-3] matches a single digit in the range 1 to 3
[^1-3] matches any character except any of the digits in the range 1 to 3
. matches any character
[.] matches the dot .
If you want to match the string [l] you should change your regex to:
System.out.println(Pattern.matches("...", "[l]"));
Now it prints true.
The regex [...] is equivalent to the regexes \. and [.].
The tutorial is a little misleading, it says:
[...] Matches any single character in brackets.
However what it means is that the regex will match a single character against any of the characters inside the brackets. The ... means "insert characters you want to match here". So you need replace the ... with the characters that you want to match against.
For example, [AP]M will match against "AM" and "PM".
If your regex is literally [...] then it will match against a literal dot. Note there is no point repeating characters inside the brackets.
The tutorial is saying:
Matches any single character in brackets.
It means you replace ... with a single character, for example [l]
These will print true:
System.out.println(Pattern.matches("[l]","l"));
System.out.println(Pattern.matches("[.]","."));
System.out.println(Pattern.matches("[.]*","."));
System.out.println(Pattern.matches("[.]*","......"));
System.out.println(Pattern.matches("[.]+","......"));

Regular expression to allow numbers, characters or alphanumeric but not empty strings or special character

I have inputs as number,alphamumeric char etc. I have build regular expression to validate this but failing to validate empty string.(empty string should not be allowed)
here is my regex,
(^([A-Za-z]|[0-9])+$)
Can this be improved with above requirement?
You just need this regex:
^[A-Za-z0-9]+$
This will only allow alphabets (uppercase or lowercase) and digits. Empty string will NOT be allowed since regex will match for input of 1 or more in length.
I have inputs as number,alphamumeric char etc. I have build regular
expression to validate this but failing to validate empty
string.(empty string should not be allowed) here is my regex,
The regex you posted already disallows the empty string. But you should combine the [A-Za-z] and [0-9] using \w like this:
^(\w)+$
If you want it to ALSO accept the empty string you could use:
^(\w)*$

regex help in java

I'm trying to compare following strings with regex:
#[xyz="1","2"'"4"] ------- valid
#[xyz] ------------- valid
#[xyz="a5","4r"'"8dsa"] -- valid
#[xyz="asd"] -- invalid
#[xyz"asd"] --- invalid
#[xyz="8s"'"4"] - invalid
The valid pattern should be:
#[xyz then = sign then some chars then , then some chars then ' then some chars and finally ]. This means if there is characters after xyz then they must be in format ="XXX","XXX"'"XXX".
Or only #[xyz]. No character after xyz.
I have tried following regex, but it did not worked:
String regex = "#[xyz=\"[a-zA-z][0-9]\",\"[a-zA-z][0-9]\"'\"[a-zA-z][0-9]\"]";
Here the quotations (in part after xyz) are optional and number of characters between quotes are also not fixed and there could also be some characters before and after this pattern like asdadad #[xyz] adadad.
You can use the regex:
#\[xyz(?:="[a-zA-z0-9]+","[a-zA-z0-9]+"'"[a-zA-z0-9]+")?\]
See it
Expressed as Java string it'll be:
String regex = "#\\[xyz=\"[a-zA-z0-9]+\",\"[a-zA-z0-9]+\"'\"[a-zA-z0-9]+\"\\]";
What was wrong with your regex?
[...] defines a character class. When you want to match literal [ and ] you need to escape it by preceding with a \.
[a-zA-z][0-9] match a single letter followed by a single digit. But you want one or more alphanumeric characters. So you need [a-zA-Z0-9]+
Use this:
String regex = "#\\[xyz(=\"[a-zA-z0-9]+\",\"[a-zA-z0-9]+\"'\"[a-zA-z0-9]+\")?\\]";
When you write [a-zA-z][0-9] it expects a letter character and a digit after it. And you also have to escape first and last square braces because square braces have special meaning in regexes.
Explanation:
[a-zA-z0-9]+ means alphanumeric character (but not an underline) one or more times.
(=\"[a-zA-z0-9]+\",\"[a-zA-z0-9]+\"'\"[a-zA-z0-9]+\")? means that expression in parentheses can be one time or not at all.
Since square brackets have a special meaning in regex, you used it by yourself, they define character classes, you need to escape them if you want to match them literally.
String regex = "#\\[xyz=\"[a-zA-z][0-9]\",\"[a-zA-z][0-9]\"'\"[a-zA-z][0-9]\"\\]";
The next problem is with '"[a-zA-z][0-9]' you define "first a letter, second a digit", you need to join those classes and add a quantifier:
String regex = "#\\[xyz=\"[a-zA-z0-9]+\",\"[a-zA-z0-9]+\"'\"[a-zA-z0-9]+\"\\]";
See it here on Regexr
there could also be some characters before and after this pattern like
asdadad #[xyz] adadad.
Regex should be:
String regex = "(.)*#\\[xyz(=\"[a-zA-z0-9]+\",\"[a-zA-z0-9]+\"'\"[a-zA-z0-9]+\")?\\](.)*";
The First and last (.)* will allow any string before the pattern as you have mentioned in your edit. As said by #ademiban this (=\"[a-zA-z0-9]+\",\"[a-zA-z0-9]+\"'\"[a-zA-z0-9]+\")? will come one time or not at all. Other mistakes are also very well explained by Others +1 to all other.

Categories