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.
Related
I am looking for a 15 characters length regex with a decimal.
In the swift documentation, the regex would look like this : 3!a15d where 3!a means [a-zA-Z]{3} and 15d means a decimal of 15 characters length with a comma.
I tried the regex below :
([A-Z]{3}[0-9]{1,14}[,][0-9]{1})|([A-Z]{3}[0-9]{1,13}[,][0-9]{1,2})|([0-9]{1,12}[,][0-9]{1,3})|([0-9]{1,11}[,][0-9]{1,4})|([0-9]{1,10}[,][0-9]{1,5})|([0-9]{1,9}[,][0-9]{1,6})|([0-9]{1,8}[,][0-9]{1,7})|([0-9]{1,7}[,][0-9]{1,8})|([0-9]{1,6}[,][0-9]{1,9})|([0-9]{1,5}[,][0-9]{1,10})|([0-9]{1,4}[,][0-9]{1,11})|([0-9]{1,3}[,][0-9]{1,12})|([0-9]{1,2}[,][0-9]{1,13})|[0-9]{1}[,][0-9]{1,14}
But it didn't work.
Do you have any tips to help me?
You can use
^[a-zA-Z]{3}(?=[^,]*,[^,]*$)\d(?:,?\d){14}$
See the regex demo.
Details:
^ - start of string
[a-zA-Z]{3} - three ASCII letters
(?=[^,]*,[^,]*$) - only one obligatory comma must be present further in the string
\d - a digit
(?:,?\d){14} - fourteen repetitions of an optional comma and a digit
$ - end of string.
Sample usage in Java to validate a string:
Boolean isValid = text.matches("[a-zA-Z]{3}(?=[^,]*,[^,]*$)\\d(?:,?\\d){14}");
This question already has an answer here:
Restricting character length in a regular expression
(1 answer)
Closed 4 years ago.
Regex: ^[a-zA-Z]+(?:[\\s'.-]*[a-zA-Z]+)*$
I want add another validation on it i.e. minimum 3 characters and maximum 15 characters.
Regex: ^([a-zA-Z]+(?:[\\s'.-]*[a-zA-Z]+)*){3,28}$
This is validating for minimum characters but not for maximum characters.
Any help is appreciated.
You could use a positive lookahead (?=.{3,15}$ to check if the string has a length from 3 - 15 characters.
Because the minimum length of the string is 3 and has to start and end with a-zA-Z you can combine the 2 character classes in the middle in this case.
I think your pattern could be simplified by removing the repetition of the group due to the positive lookahead to:
^(?=.{3,15}$)[a-zA-Z]+[\\s'.a-zA-Z-]*[a-zA-Z]+$
Explanation
^ Start of the string
(?=.{3,15}$) Positive lookahead to assert the lenght 3-15
[a-zA-Z]+ Match 1+ times a lower/upper case char a-z
[\\s'.a-zA-Z-]* Charater class to match any of the listed 0+ times
[a-zA-Z]+ Match 1+ times a lower/upper case char a-z
$ End of the string
See the Java demo
I'm writing a simple code in java/android.
I want to create regex that matches:
0
123
123,1
123,44
and slice everything after second digit after comma.
My first idea is to do something like that:
^\d+(?(?=\,{1}$)|\,\d{1,2})
^ - from begin
\d+ match all digits
?=\,{1}$ and if you get comma at the end
do nothin
else grab two more digits after comma
but it doesn't match numbers without comma; and I don't understand what is wrong with the regex.
You may use
^(\d+(?:,\d{1,2})?).*
and replace with $1. See the regex demo.
Details:
^ - start of string
-(\d+(?:,\d{1,2})?) - Capturing group 1 matching:
\d+ - one or more digits
(?:,\d{1,2})? - an optional sequence of:
, - a comma
\d{1,2} - 1 or 2 digits
.* - the rest of the line that is matched and not captured, and thus will be removed.
basic regex : [0-9]+[, ]*[0-9]+
In case you want to specify min max length use:
[0-9]{1,3}[, ]*[0-9]{0,2}
Here:
,{1}
says: exactly ONE ","
Try:
,{0,1}
for example.
I got a string of an equation where I want to replace all occurrences of the scheme "x^2" with "x²".
My code:
String equation = "x^2";
equation = equation.replace("^2", "\u00B2"); // u00b2 is unicode for '²'
System.out.println(equation);
This works for "x^2" but for example "x^25" I'm getting the string "x²5", but in such a case I want it to stay the same "x^25".
Another example:
"x^2 + 6x" -> "x² + 6x" // ... x squared
"x^28 + 6x" -> "x^28 + 6x" // ... x to the power of 28
Thank you!
EDIT:
The solution from "Mshnik" works perfectly, even with a custom character like "y^2" instead of "x^2", thanks!
Here's a regex that will match 2 in x^2, the 2 in a^2+... but not the 2 in x^20:
(?<=\w)\^2(?![0-9.])
Specifically:
(?<= <EXP>) is a positive lookbehind on <EXP>, More explanation here
\w matches any alphabetic character, upper or lower case.
\^ matches the ^ character literally
2 matches the 2 character literally
(?! <EXP>) is a negative lookahead on <EXP> More explanation here.
[0-9.] matches all numbers and decimals, like 58 and 3.14.
Thus all together it matches the 2 that is preceded by x^ and isn't followed by a digit.
With that, you can use java's Pattern class to find and rebuild a string with the new ². More on that here
Note that in order to get a backslash into a java regex, you need the literal backslash character represented by \\. Thus the final result looks like (?<=\\w)\\^2(?![0-9.]).
I have a need to validate the Currency String as followings:
1. The Currency Unit must be in Uppercase and must contain 3 characters from A to Z
2. The number can contain negative (-) or positive (+) sign.
3. The number can contain the decimal fraction, but if the number contain
the decimal fraction then the fraction must be 2 Decimal only.
4. There is no space in the number part
So see this example:
10 USD ------> match
+10 USD ------> match
-10 USD ------> match
10.23 AUD ------> match
-12.11 FRC ------> match
- 11.11 USD ------> NOT match because there is space between negative sign and the number
10 AUD ------> NOT match because there is 2 spaces between the number and currency unit
135.1 AUD ------> NOT match because there is only 1 Decimal in the fraction
126.33 YE ------> NOT match because the currency unit must contain 3 Uppercase characters
So here is what I tried but failed
if(text != null && text.matches("^[+-]\\d+[\\.\\d{2}] [A-Z]{3}$")){
return true;
}
The "^\\d+ [A-Z]{3}$" only match number without any sign and decimal part.
So Can you fix this Java Regex to match currency that meets the above requirements?
Some other questions in the internet do not match my requirements.
It seems you don't know about ? quantifier which means that element which this quantifier describes can appear zero times or once, making it optional.
So to say that string can contain optional - or + at start just add [-+]?.
To say that it can contain optional decimal part in form .XX where X would be digit just add (\\.\\d{2})?
So try with "^[-+]?\\d+(\\.\\d{2})? [A-Z]{3}$"
BTW If you are using yourString.matches(regex) then you don't have to add ^ or $ to regex. This method will match only if entire string will match regex so these metacharacters are not necessary.
BTW2 Normally you should escape - in character class [...] because it represents range of characters like [A-Z] but in this case - can't be used this way because it is at start of character class so there is no "first" range character, so you don't have to escape - here. Same goes if - is last character in [..-]. Here it also can't represent range so it is simple literal.
Try with:
text.matches("[+-]?\\d+(\\.\\d\\d)? [A-Z]{3}")
Note that since you use .matches(), the regex is automatically anchored (blame the Java API desingers for that: .matches() is woefully misnamed)
you could start your regex with
^(\\+|\\-)?
Which means that it will accept either one + sign, one - sign or nothing at all before the digit. But that's only one of your problems.
Now the decimal point:
"3. The number can contain the decimal fraction, but if the number contain
the decimal fraction then the fraction must be 2 Decimal only."
so after the digit \\d+ the next part should be in ( )? to indicate that it is optional (meaning 1 time or never). So either there are exactly one dot and two digits or nothing
(\\.\\d{2})?
Here you can find a reference for regex and test them. Just have a look at what else you could use to identify the 3 Letters for the currency. E.g. the \s could help you to identify a whitespace
This will match all your cases:
^[-+]?\d+(\.\d{2})?\s[A-Z]{3}$
(Demo # regex101)
To use it in Java you have to escape the \:
text.matches("^[-+]?\\d+(\\.\\d{2})?\\s[A-Z]{3}$")
Your regex wasn't far from the goal, but it contains several mistakes.
The most important one is: [] denotes a character class while () is a capturing group. So when you specify a character group like [\\.\\d{2}] it will match on the characters \,.,d,{,2, and}, while you want to match on the pattern .\d{2}.
The other answers already taught you the ? quantifier, so I won't repeat this.
On a sidenote: regular-expressions.info is a great source to learn these things!
Explanation of the regex used above:
^ #start of the string/line
[-+]? #optionally a - or a + (but not both; only one character)
\d+ #one or more numbers
( #start of optional capturing group
\.\d{2} #the character . followed by exactly two numbers (everything optional)
)? #end of optional capturing group
\s #a whitespace
[A-Z]{3} #three characters in the range from A-Z (no lowercase)
$ #end of the string/line