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
Related
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.
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.
How can I create a regex that detects any string starting with two consecutive numbers followed by at the most nine consecutive characters with a hyphen symbol in java regex? For instance:
nnccccccccc-nnccccccccc
or
nncccccc-nnccccccccc
or
nnccccccc-nncccccccc
Where n represents a number from 0 to 1 and c a letter character.
So far I tried this: https://regex101.com/r/a1eJvY/2.
You may use this regex for your matches:
^\d{1,2}[a-zA-Z]{1,9}-\d{1,2}[a-zA-Z]{1,9}$
RegEx Demo
If you are using .matches() method then ^ and $ are not needed.
\d{1,2}: Match 1 or 2 digits
[a-zA-Z]{1,9}: Match 1 to 9 English letters
-: Match literal hyphen
You can use ^(\d{2}[a-zA-Z]{0,9})-(\d{2}[a-zA-Z]{0,9})$ example: https://regex101.com/r/A2wiHH/2.
This will match the string as described below:
The beginning of your string
2 decimals
0-9 characters
-,
2 decimals again,
0-9 characters again
The end of your string
I suppose that "nn" and no one letter char is accepted, so, for a single sequence:
[0,1]{2}\D{0,9}
Explaination:
[0,1]{2} --> Accept only 0 and 1 as number exactly two times;
\D{0,9} --> Accept 0 to 9 generic number.
Edit: you said
Where n represents a number from 0 to 1
but if 22may is accepted, you want number from 0 to 9, so you have to use \d
\d{2}\D{0,9}
Try this
[0-1] {2}+ [a-z] {9}. +-
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
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}";