REGEX extract two double number separated from hypen - java

I have strings like:
some foo text
some foo
1-2
1.00-2.00
3.21-1.23
2.12-2.12
I have to check if the string format contains two numbers separated by hyphen.
How can I do it?
Thanks

Regex for float is: ^[1-9]\d*\.\d+$ if decimals are optional : ^[1-9]\d*(?:\.\d+)?$
Repeat it twice with hyphen in between:
`^[1-9]\d*(?:\.\d+)?-[1-9]\d*(?:\.\d+)?$`

You can use the regex:
^\d+(\.\d+)?-\d+(\.\d+)?$
Explanation can be found here.
Using java you can create a method that checks whether your desired pattern exists or not:
public static boolean returnMatch(String input) {
Pattern p1 = Pattern.compile("^\\d+(\\.\\d+)?-\\d+(\\.\\d+)?$");
Matcher m1 = p1.matcher(input);
return m1.find() ? true : false;
}
Now call it using:
System.out.println(returnMatch("some foo text")); // false
System.out.println(returnMatch("1.00-2.00")); // true
System.out.println(returnMatch("2.12-2.12")); // true
System.out.println(returnMatch("10-20")); // true

Use a simple Regex:
(\d+(?:\.\d+)?)-(\d+(?:\.\d+)?)
This solution assumes there is always a decimal part present (at least one digit). Demo at Regex101.
\d is a digit
\d+ is at least one digit
\. matches a dot (.) literally
() is a capturing group
(?:\.\d+)? is a non-capturing group which optionally matches the decimal part
Don't forget the proper escaping in Java String regex = "(\\d+(?:\\.\\d+)?)-(\\d+(?:\\.\\d+)?)";
In case one or more spaced or blank characters appear between the dash and numbers, use:
(\d+(?:\.\d+)?)\s*-\s*(\d+(?:\.\d+)?)

Related

Regex to mask multiple phone numbers (~) separated except last 4 digiits

I am trying to find a regex which masks phone numbers except last 4 digits.
example: phone=9988998888~7654321908~6789054321
Desired output : phone=******8888~******1908~*****4321
I tried below regex but it is masking only starting number
phone=******8888~7654321908~6789054321
^(phone)=(\d(?=\d{4}))*
Use replaceAll​(Function<MatchResult,​String> replacer) to replace each digit in MatchResult with "*".
public class PhoneNumberMask {
public static void main(String[] args) {
String target = "phone=9988998888~7654321908~6789054321";
Pattern pattern = Pattern.compile("(\\d+(?=\\d{4}))");
Matcher matcher = pattern.matcher(target);
String result = matcher.replaceAll((matchResult) -> matchResult.group(1).replaceAll("\\d", "*"));
System.out.println(result);
}
}
You could use:
\d(?=\d{4})
See this online demo
\d - Any single digit.
(?=\d{4}) - Positive lookahead for 4 digits.
Replace with *.
See a Java demo
Assuming you only want to mask all numbers in a string that starts with phone= separated with ~, you can use a plain regex solution without a lambda in the replacement with
String masked = text.replaceAll("(\\G(?!^)(?:\\d{4}~)?|^phone=)\\d(?=\\d{4})", "$1*");
See the regex demo. Details:
(\G(?!^)(?:\d{4}~)?|^phone=) - Group 1: end of the previous successful match and then an optional sequence of four digits and a ~ or start of string and phone=
\d - a digit
(?=\d{4}) - followed with any four digits.

Match float or integer number

I'm trying to check is a String fits the requirements.
Now I have something like this:
String aaa = "322.155";
boolean match = aaa.matches("\\d{3}\\.\\d{3}"); //matches
String aaa = "32.155";
boolean match = aaa.matches("\\d{3}\\.\\d{3}"); //don't match
What I want is make all this numbers
1
2
999
22.22
333.23
1.012
match the pattern.
What pattern should I use?
boolean match = aaa.matches("\\d{1-3}\\.\\d{1-3}"); //isn't correct
Another possibility:
"\\d{1,3}(\\.\\d{1,3})?"
Matches one to three digits and, optionally, a decimal with another one to three trailing digits.
1 matches
2 matches
999 matches
22.22 matches
333.23 matches
1.012 matches
.123 does not match
1234 does not match
123.1234 does not match
1..2 does not match
As the . is not mandatory
\\d+\\.?\\d+
"\\d{1,3}\\.?\\d{0,3}"
Requires at least one number before the ., which is optional and up to three numbers behind.
Just use a non-specific quantifier:
"\\d+?\\.?\\d*"
This will match any 1+ digits, followed by a dot, followed by any 0+ digits.
Test
String[] test = {"1", "10", "1.1", "100.1000", "......................1", "0..2"};
for (String s: test) {
System.out.println(s.matches("\\d+?\\.*\\d*"));
}
Output
true
true
true
true
false
false
Note
As the requirements are not too clear, if you need to cap the number of digits to match either before or after the separator, you can use the {min,max} quantifier idiom as explained by hotzst, instead of a general quantifier.

Writing regex for string containing no only numbers

I need to write a regex containing not only digits [0-9]. How can I do that without explicitly specifying all possible charaters in a group. Is it possible to do through lookahead/lookbehind? Examples:
034987694 - doesn't match
23984576s9879 - match
rtfsdbhkjdfg - match
=-0io[-09uhidkbf - match
9347659837564983467 - doesn't match
^(?!\\d+$).*$
This should do it for you.See demo.
https://regex101.com/r/fM9lY3/1
The negative will lookahead will check if the string doesnt have integers from start to end.You need $ to make sure the check is till end or else it will just check at the start.
If you just need to detect whether the string is not numbers-only, then you can simply test for /\D/ - "succeed if there is a non-digit anywhere".
Why not check if it only contains digits, if not it matches
String[] strings = {"034987694", "23984576s9879",
"rtfsdbhkjdfg",
"=-0io[-09uhidkbf",
"9347659837564983467"};
for (String s : strings) {
System.out.printf("%s = %s%n", s, !s.matches("\\d*"));
}
output
034987694 = false
23984576s9879 = true
rtfsdbhkjdfg = true
=-0io[-09uhidkbf = true
9347659837564983467 = false
You may try the below,
string.matches(".*\\D.*");
This expects atleast 1 non-digit character.

Perl5Matcher.matches(input, pattern) is returning true for input containing semicolon even when semicolon is not in pattern

I have a string MyString = "AP;"; or any other number of strings containing ;
When I attempt to validate that MyString matches a pattern
eg. MyPattern = "^[a-zA-Z0-9 ()+-_.]*$";
Which I believe should allow AlphaNumerics, and the characters ()+-_.]* but not ;
However the below statement is returning True!
Pattern sepMatchPattern = sepMatchCompiler.compile("^[a-zA-Z0-9 ()+-_.]*$");
Perl5Matcher matcher = new Perl5Matcher();
if (matcher.matches("AP;", sepMatchPattern)) {
return true;
} else {
return false;
}
Can anyone explain why the semicolon keeps getting allowed through?
The problem lies in the regular expression that you have defined - ^[a-zA-Z0-9 ()+-_.]*$. Within this regular expression is a character class of alpha (upper and lower), numeric, space, parentheses, and some punctuation. One of the punctuation characters is a period. The period is not escaped, and thus it has its original meaning of any character (including a semi colon).
This regex will match any string - it is essentially ^.*$.
To fix this, escape the period.
Pattern sepMatchPattern = sepMatchCompiler.compile("^[a-zA-Z0-9 ()+-_\\.]*$");
Edit:
It turns out that there is another item that I missed in there that has special meaning. The hyphen in the character class of "+-_" does not mean "plus, hyphen, or underscore". Rather, it means all the characters from 0x2B to 0x5F (inclusive). A quick test shows that ^[+-_]*$ also matches AP; because A and P are 0x41 and 0x50 and the notorious semicolon is 0x3B - all within the range of 0x2B to 0x5F.
The correct regular expression is:
"^[a-zA-Z0-9 ()+\\-_\\.]*$"

Regex to get first number in string with other characters

I'm new to regular expressions, and was wondering how I could get only the first number in a string like 100 2011-10-20 14:28:55. In this case, I'd want it to return 100, but the number could also be shorter or longer.
I was thinking about something like [0-9]+, but it takes every single number separately (100,2001,10,...)
Thank you.
/^[^\d]*(\d+)/
This will start at the beginning, skip any non-digits, and match the first sequence of digits it finds
EDIT:
this Regex will match the first group of numbers, but, as pointed out in other answers, parseInt is a better solution if you know the number is at the beginning of the string
Try this to match for first number in string (which can be not at the beginning of the string):
String s = "2011-10-20 525 14:28:55 10";
Pattern p = Pattern.compile("(^|\\s)([0-9]+)($|\\s)");
Matcher m = p.matcher(s);
if (m.find()) {
System.out.println(m.group(2));
}
Just
([0-9]+) .*
If you always have the space after the first number, this will work
Assuming there's always a space between the first two numbers, then
preg_match('/^(\d+)/', $number_string, $matches);
$number = $matches[1]; // 100
But for something like this, you'd be better off using simple string operations:
$space_pos = strpos($number_string, ' ');
$number = substr($number_string, 0, $space_pos);
Regexs are computationally expensive, and should be avoided if possible.
the below code would do the trick.
Integer num = Integer.parseInt("100 2011-10-20 14:28:55");
[0-9] means the numbers 0-9 can be used the + means 1 or more times. if you use [0-9]{3} will get you 3 numbers
Try ^(?'num'[0-9]+).*$ which forces it to start at the beginning, read a number, store it to 'num' and consume the remainder without binding.
This string extension works perfectly, even when string not starts with number.
return 1234 in each case - "1234asdfwewf", "%sdfsr1234" "## # 1234"
public static string GetFirstNumber(this string source)
{
if (string.IsNullOrEmpty(source) == false)
{
// take non digits from string start
string notNumber = new string(source.TakeWhile(c => Char.IsDigit(c) == false).ToArray());
if (string.IsNullOrEmpty(notNumber) == false)
{
//replace non digit chars from string start
source = source.Replace(notNumber, string.Empty);
}
//take digits from string start
source = new string(source.TakeWhile(char.IsDigit).ToArray());
}
return source;
}
NOTE: In Java, when you define the patterns as string literals, do not forget to use double backslashes to define a regex escaping backslash (\. = "\\.").
To get the number that appears at the start or beginning of a string you may consider using
^[0-9]*\.?[0-9]+ # Float or integer, leading digit may be missing (e.g, .35)
^-?[0-9]*\.?[0-9]+ # Optional - before number (e.g. -.55, -100)
^[-+]?[0-9]*\.?[0-9]+ # Optional + or - before number (e.g. -3.5, +30)
See this regex demo.
If you want to also match numbers with scientific notation at the start of the string, use
^[0-9]*\.?[0-9]+([eE][+-]?[0-9]+)? # Just number
^-?[0-9]*\.?[0-9]+([eE][+-]?[0-9]+)? # Number with an optional -
^[-+]?[0-9]*\.?[0-9]+([eE][+-]?[0-9]+)? # Number with an optional - or +
See this regex demo.
To make sure there is no other digit on the right, add a \b word boundary, or a (?!\d)
or (?!\.?\d) negative lookahead that will fail the match if there is any digit (or . and a digit) on the right.
public static void main(String []args){
Scanner s=new Scanner(System.in);
String str=s.nextLine();
Pattern p=Pattern.compile("[0-9]+");
Matcher m=p.matcher(str);
while(m.find()){
System.out.println(m.group()+" ");
}
\d+
\d stands for any decimal while + extends it to any other decimal coming directly after, until there is a non number character like a space or letter

Categories