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.
Related
I have this String and I want to filter the digit that came after the big number with the space, so in this case I want to filter out 2 and 0.32. I used this regex below which only filters out decimal numbers, however I want to filter both decimals and integer numbers, is there any way?
String s = "ABB123,ABPP,ADFG0/AA/BHJ.S,392483492389 2,BBBB,YUIO,BUYGH/AA/BHJ.S,3232489880 0.32"
regex = .AA/BHJ.S,\d+ (\d+.?\d+)
https://regex101.com/r/ZqHDQ8/1
The problem is that \d+.?\d+ matches at least two digits. \d+ matches one or more digits, then .? matches any optional char other than line break char, and then again \d+ matches (requires) at least one digit (it matches one or more).
Also, note that all literal dots must be escaped.
You can use
.AA/BHJ\.S,\d+\s+(\d+(?:\.\d+)?)
See the regex demo.
Details:
. - any one char
AA/BHJ\.S, - a AA/BHJ.S, string
\d+ - one or more digits
\s+ - one or more whitespaces
(\d+(?:\.\d+)?) - Group 1: one or more digits, and then an optional sequence of a dot and one or more digits.
You could look for anything following /AA/BHJ with a reluctant quantifier, then use a capturing group to look for either digits or one or more digits followed by a decimal separator and other digits.
/AA/BHJ.*?\s+(\d+\.\d+|\d+)
Here is a link to test the regex:
https://regex101.com/r/l5nMrD/1
I want to match the lines having specific count of decimal numbers separated by spaces. Say lines having only 3 decimal numbers. Consider the below example:
Abc 1.56 1.67 5.67
xyz 4.51 12.43 32.50
03/31/2019 $1234 $(1234) $60,501 5.81 7.81
abcdf $123,345 $123 $123,149
For this given input I want to fetch only the first two lines as they have only contains 3 decimal numbers separated by space. I have tried (.*[\s0-9.]+)$ && ([\s0-9.]+)$ but with these I end up getting many other unwanted lines as well.Could some one please advise if this is something we can do using regular expression.
Using [\s0-9.]+ is a broad match for a decimal number, as it could also match only newlines, spaces or dots.
If there can also digits occur before the decimals, you could use:
^.*?\d+\.\d+(?:\h+\d+\.\d+){2}$
Explanation
^ Start of string
.*? Match any char except an newline non greedy
\d+\.\d+ Match a decial number
(?:\h+\d+\.\d+){2} Repeat 2 times matching 1+ horizontal whitespace chars and a decimal number
$ End of string
In Java
String regex = "^.*?\\d+\\.\\d+(?:\\h+\\d+\\.\\d+){2}$";
Regex demo
If the digit before the decimal is optional, you could change it to \d*\.\d+
To match lines starting with some text, then three decimal numbers separated by spaces, use this regex:
(?m)^(\p{L}+)\h+(-?[\d.]+)\h+(-?[\d.]+)\h+(-?[\d.]+)\h*$
Remember to double the \ when inserting into a Java string literal.
See regex101 for demo.
I have the following scenario, I don't seem to solve this.
String = "a var a12nd with code is on467th";
in the above string i am required to match words with numbers in it and matches the numbers in another group, can you please help?
expected output:
match1: a12nd
group1: 12
mach2: on467th
group1:467
Try this one, it matches any letter followed by any number followed by any letter and it is storing number in a group.
it also maches if a number is at the start and end of the word
([a-z]*([\d]+)[a-z]*)
try demo here
This one should work
[a-z]+(\d+)[a-z]+
This translates to match :
Any white space followed by alphabets followed by a number (you want) followed by alphabets.
Here group 1 would be your number and group 0 would be the string containing the number.
See this in action.
I'm trying to make a regex pattern for phone numbers like this format : 999-999-9999
So I tried this expression :
\+\d(-\d{3}){2}-\d{4}
But the format works on numbers like : +1-999-999-9999
I would appreciate if someone help me modifying the pattern to make it match the format 999-999-9999 , which means I don't want the user to enter the + sign at first of the string
Thank you
The expression \+\d(-\d{3}){2}-\d{4} you have tried explicitly says:
A + followed by a single digit followed by a hyphen and 3 digits (two times) followed by 4 digits.
If you don't want the + and the first single digit, don't include those in your regular expression.
This expression \d{3}-\d{3}-\d{4} says:
3 digits followed by a hyphen followed by 3 digits followed by a hyphen followed by 4 digits.
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.