Regex to match lines having specific count of Decimal numbers - java

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.

Related

Given string filter out unique element from string using regex

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

Regex - pattern for allowing only numbers

I am trying to get a REGEX which allows only number starts from 1-9 and only of 4 characters and should can only have a zero after decimal.
This is what i have written so far, but it fails every time.
^[1-9]\\d{4,4}(\\.\\d{1,1})?%?$
I am using this as a validation in java while reading an excel sheet.
For your given requirements you may use this regex:
final String regex = "^[1-9]\\d{3}(?:\\.0)?$";
RegEx Breakup
^: Start
[1-9]: Match a digit 1-9 at start
\\d{3}: Match any 3 digits
(?:\\.0)?: Match optional .0 at the end
$: End

Regular expression to match until last 3 characters before a comma

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.

How to remove numbers from string which starts and ends with numbers?

I have two strings
1. J2EE
2. java1.6
I want to remove numbers from only the start and ends; not from between.
Can anyone provide a regular expression for this, or any other solution?
simple using replaceAll() using ^\\d+|\\d+$ regex that looks for digits in the beginning and ending of the line.
System.out.println("1adfds23dfdsf121".replaceAll("^\\d+|\\d+$", ""));
output:
adfds23dfdsf
EDIT
Regex explanation:
^ Start of line
\d+ Any digit (one or more times)
| OR
\d+ Any digit (one or more times)
$ End of line
Use this:
String replaced = yourString.replaceAll("^\\d+\\. |(?:\\d+\\.)?\\d+$", "");
Output:
J2EE
java
Explanation
^\d+\. matches the leading digits, period and space
'|' OR
(?:\d+\.)?\d+$ matches optional digits-and-dot, digits, and end of string
Replace with the empty string
Use this:
string.replaceAll("^(?:\\d|\\d\\.\\d)*|(?:\\d|\\d\\.\\d)*$", "");
This looks for digits at the end and/or start of the string, and replaces them with nothing. It will also remove decimal dots (.)
^ Start of string
(?:\\d|\\d+\\.\\d+)* a digit or a number with a decimal (1234.4321)
* zero or more times
| or
$ end of the string

Can you fix this Java Regex to match currency such as -10 USD, 12.35 AUD ... (Java)?

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

Categories