I want to ask Regular expression
I have a a float number called 0.11,
in this case, the first digit must be Zero
and there can be at most three decimal digits , like 0.1, 0.11, 0.111
In Java, I code like this
String phone_regex = "d{1,1}.d{1,3}";
But it does not work...
Can someone provide some suggestion for me?
thank you
Try escaping your digit character group \\d, e.g.,
"^0\\.\\d{1,3}$"
This matches all strings beginning with a 0, followed by a dot and 1-3 digits.
^ means beginning of line, $ means end of line. See also java.util.Pattern.
You need to escape the numbers class (\d) and the . char.
String phone_regex = "\\d\\.\\d{1,3}";
Related
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 am building on the idea of [Jonah][1]: Matching to one decimal place in Java using regex
^([1-9]\d*|0)(\.\d)?$
This regex is the one I want. But my additional requirement is that the decimal digit must be an even number such as 0.2, 10.4, 100.6, 11.8.
I am thinking of splitting the string at '.' and checking if decimal digit % 2 = 0. How can I achieve this in regex?
Instead of using the /d for a decimal digit, you can just explicitly require an even number, like this:
^([1-9]\d*|0)(\.[24680])?$
Maybe this is asked somewhere but certainly I couldn't find the answer I want so:
I'm having difficulties to match specific characters in a string:
"88551554,86546546,51516565"
The digits I want to match are the X's in the following :
"XXXXX554,XXXXX546,XXXXX565"
Right now I'm only able to find out the last 3 digits before each comma :
\d{3}(?=,)
And since the length of the numbers are dynamic, it seems not possible to specify the number of digits before the 3 digits.
Anyone can help?
Thanks in advance!
You can use this lookahead regex:
(\d+)(?=\d{3}(?:,|$))
RegEx Demo
This will match and group 1 or more digits that must be followed by 3 digits and a comma or end of input. Check MATCH INFORMATION in the demo link for captured groups.
Update: To replace all those matched digits by X use:
str = str.replaceAll("\\d(?=\\d*\\d{3}(?:,|$))", "X");
RegEx Demo2
To match it use:
\d+(?=\d{3})
This regex does:
\d+... Match a digit (0-9) between one and unlimmited times.
(?=\d{3}) ... Match a digit (0-9) exactly three times inside an positive lookahead.
I am trying to write a regex for java that will match the following string:
number,number,number (it could be this simple or it could have a variable number of numbers, but each number has to have a comma after it there will not be any white space though)
here was my attempt:
[[0-9],[0-9]]+
but it seems to match anything with a number in it
You could try something along the lines of ([0-9]+,)*[0-9]+
This will match:
Only one number, e.g.: 7
Two numbers, e.g.: 7,52
Three numbers, e.g.: 7,52,999
etc.
This will not match:
Things with spaces, e.g.: 7, 52
A list ending with a comma, e.g.: 7, 52,
Many other things out of the scope of this problem.
I think this would work
\d+,(\d+,)+
Note that as you want, that will only capture number followed by a comma
I guess you are starting with a String. Why don't you just use String.split(",") ?
^ means the start of a string and $ means the end. If you don't use those, you could match something in the middle (b matched "abc").
The + works on the element before it. b is an element, [0-9] is an element, and so are groups (things wrapped in parenthesis).
So, the regex you want matches:
The start of the string ^
a number [0-9]
any amount of comas flowed by numbers (,[0-9])+
the end of the string $
or, ^[0-9](,[0-9])+$
Try regex as [\d,]* string representation as [\\d,]* e.g. below:
Pattern p4 = Pattern.compile("[\\d,]*");
Matcher m4 = p4.matcher("12,1212,1212ad,v");
System.out.println(m4.find()); //prints true
System.out.println(m4.group());//prints 12,1212,1212
If you want to match minimum one comma (,) and two numbers e.g. 12,1212 then you may want to use regex as (\d+,)+\d+ with string representation as \\d+,)+\\d+. This regex matches a a region with a number minimum one digit followed by one comma(,) followed by minimum one digit number.
Can someone tell me what the regular expression in the following Java code snippet means:
String someString = …;
someString.matches("^\\d{5}-\\d{4}$");
This will match 5 decimal numbers at the beginning of the string, followed by a dash, followed by 4 decimal numbers at the end.
^ = Beginning of string
\d{n} = Match n decimal numbers
$ = End of string
From http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
five digits, a dash, and then four more digits...nothing else
^ means beginning of line.
\d{5} means five digits.
- literally means "-"
\d{4} means four digits.
$ means end of line.
So it's looking for a sequence of five digits followed by a sequence of four digits, seperated by a dash and that is the only thing on the line.
Example:
12345-6789