Can someone explain regex this regex expression [duplicate] - java

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
Hi there I'm new to Java and was going through some information on regex and I couldn't comprehend this the following expression:
"^[a-zA-Z\-]+$"
Could someone be kind enough to explain each and every character in this expression?
Thank you.

^ $ # Check if the entire string matches,
[ ]+ # with one or more of the following characters:
a-z # Any lowercase (ASCII) letter
A-Z # Any uppercase (ASCII) letter
\- # Or an "-" (the `\` is used to escape it)
Or in short: this regex checks if a given string consists solely of (ASCII) letters and/or -, and is non-empty.
Try it online.

[a-zA-Z] means all characters a through or A through Z, inclusive.
The "\" inside the square bracket is used as an escape character.
Symbol "+" in the end signified that your regex can occur once or more times.

Related

Having difficulty understanding Java regex interpretation [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
Can someone help me with the following Java regex expression? I've done some research but I'm having a hard time putting everything together.
The regex:
"^-?\\d+$"
My understandning of what each symbol does:
" = matches the beginning of the line
- = indicates a range
? = does not occur or occurs once
\\d = matches the digits
+ = matches one or more of the previous thing.
$ = matches end of the line
Is the regex saying it only want matches that start or end with digits? But where do - and ? come in?
- only indicates a range if it's within a character class (i.e. square brackets []). Otherwise, it's a normal character like any other. With that in mind, this regex matches the following examples:
"-2"
"3"
"-700"
"436"
That is, a positive or negative integer: at least one digit, optionally preceded by a minus sign.
Some regex is composed, as you have now, the correct way to read your regex is :
^ start of word
-? optional minus character
\\d+ one or more digits
$ end of word
This regex match any positive or negative numbers, like 0, -15, 558, -19663, ...
Fore details check this good post Reference - What does this regex mean?
"^-?\\d+$" is not a regex, it's a Java string literal.
Once the compiler has parsed the string literal, the string value is ^-?\d+$, which is a regex matching like this:
^ Matches beginning of input
- Matches a minus sign
? Makes previous match (minus sign) optional
\d Matches a digit (0-9)
+ Makes previous match (digit) match repeatedly (1 or more times)
$ Matches end of input
All-in-all, the regex matches a positive or negative integer number of unlimited length.
Note: A - only denotes a range when inside a [] character class, e.g. [4-7] is the range of characters between '4' and '7', while [3-] and [-3] are not ranges since the start/end value is missing, so they both just match a 3 or - character.

What is the correct way to make a regex that accepts all letters and a selection of characters?

I need a Regex that accepts all letters(lowercase & uppercase), numbers and these characters/symbols ('-','_','#','.'). It is not required to be in the form of an Email address. The characters can be positioned anywhere in the word. It also should not accept spaces and the word length must be 8 or more.
This is what I have so far.
^(?=\S{8})[a-zA-Z]\w*(?:\.\w+)*(?:#\w+\.\w{2,4})?$
You may use the following regex:
^[a-zA-Z0-9_#.-]{8,}$
Details
^ - start of string
[a-zA-Z0-9_#.-]{8,} - 8 or more ASCII letters, digits, ., _, # or -
$ - end of string.
See the regex demo.
Watch out for \w in Android, it matches all Unicode letters and digits by default (but not in Java).
In Android/Java, when using it with .matches(), you may remove the ^ and $ anchors as this method requires a full string match:
String regex = "[a-zA-Z0-9_#.-]{8,}";
[a-zA-Z._#\d-]{8,}
should do the trick. With additional boundaries that'd be ^[a-zA-Z._#\d-]{8,}$.
^ Beginning of the line
[a-zA-Z._#\d-] Group of the characters a-z, A-Z and ., -, _, # as per your question as well as numbers (\d)
{8,} 8 to unlimited times
$ End of the word
You can try it out on regex101.com here.
An even shorter solution would be [\w.#-]{8,} using \w as shortcut for [a-zA-Z0-9_]. regex101 Not correct! Thanks to #Wiktor Stribiżew for the correction; see comments for more.
Here's another form for alphanumeric character:
^[[:alnum:]._#\-]{8,}$

Regex for arithmetic expression [duplicate]

This question already has answers here:
In a java regex, how can I get a character class e.g. [a-z] to match a - minus sign?
(5 answers)
Closed 5 years ago.
The regex -?\d+ [+|-|*|/] -?\d+ matches expression 1 + 3 without any problems also 1 + -2 without any problems, but I don't know why it does not match 1 - 2. Could you explaing why it does not match the - char correctly?
By my regex I wanted to achieve:
optional - at the beginning
string of digits
whitespace then operator then whitespace
optional - before second stringof digits
A - unescaped in the middle of a character class creates a range. You can escape it or move it to the start or end of the character class. You also don't need/want the |s I'd guess.
You currently make a range between | and | which doesn't really make sense. You also could just use grouping instead of a character class.
(\+|-|\*|/)
With this approach the + and * need to be escaped because they are quantifiers when outside a character class.

Escape Sequence vs. Whitespace Character (\s) [duplicate]

This question already has answers here:
Version difference? Regex Escape in Java
(2 answers)
Closed 1 year ago.
Are escape sequences and whitespace characters the same thing? I'm not sure what else to write here but Stackoverflow said the first sentence is not enough so I'm typing this second sentence for no reason at all but that so this post will go through.
There are a few escape sequences specified in Java, of which \s is not part. The \s is recognized as whitespace in regular expressions, where it is a predefined character class.
Check the following sections from the Java Tutorial:
Escape Sequences
Predefined Character Classes

This regular expression is for which type of strings [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
How can I know that any particular regular expression matches which type of input? Like I want to know about \$\{([\w]+)\}. Which string will be matched by this regular expression?
Pattern placeholder = Pattern.compile("\\$\\{([\\w]+)\\}");
Matcher mat = placeholder.matcher("input");
while (mat.find()) {
}
It accepts E.L access type to variables:
${somethingHere}
As comemnted above, you can check that Reference for more info.
This will find any character within ${}
The \w metacharacter is used to find a word character.
A word character is a character from a-z, A-Z, 0-9, including the _ (underscore) character.
The other characters are escaped by \, \$ looks for a $ \{ looks for { and \} looks for }
The + token mean to repeat the character ([\w]) between one and unlimited times, as many times as possible.

Categories