Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Does anybody know what this means in java:
testNumber.matches("^800[25-9][2-9].*$")
I am mainly confused with [25-9].
[] is a subset. Any character in the brackets will match. 5-9 is a range of characters from 5 to 9. 2 is also a character. So [25-9] will match 2, 5, 6, 7, 8, and 9.
[25-9] means any character from 2 or 5-9
You can get explanation of your regex by yourself from here: http://rick.measham.id.au/paste/explain.pl
Here is a explaination about you Regular Expression:
/^800[25-9][2-9].*$/
^ assert position at start of the string
800 matches the characters 800 literally
[25-9] match a single character present in the list below
2 the literal character 2
5-9 a single character in the range between 5 and 9
[2-9] match a single character present in the list below
2-9 a single character in the range between 2 and 9
.* matches any character (except newline)
Quantifier: Between zero and unlimited times, as many times as
possible, giving back as needed [greedy]
$ assert position at end of the string
Based on http://regex101.com/ and https://www.debuggex.com/
^800[25-9][2-9].*$
^ assert position at start of the string
800 matches the characters 800 literally
[25-9] match a single character present in the list below
2 the literal character 2
5-9 a single character in the range between 5 and 9
[2-9] match a single character present in the list below
2-9 a single character in the range between 2 and 9
.* matches any character (except newline)
* Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
$ assert position at end of the string
In other words [...] is character class. It means it matches one of characters placed inside it like [123] means it matches either 1 2 3. You can also write this range as [1-3].
In case of [25-9] it means it will match 2 or one of digits in rage 5-9 which are 5, 6, 7, 8, 9.
Related
This question already has an answer here:
Restricting character length in a regular expression
(1 answer)
Closed 4 years ago.
Regex: ^[a-zA-Z]+(?:[\\s'.-]*[a-zA-Z]+)*$
I want add another validation on it i.e. minimum 3 characters and maximum 15 characters.
Regex: ^([a-zA-Z]+(?:[\\s'.-]*[a-zA-Z]+)*){3,28}$
This is validating for minimum characters but not for maximum characters.
Any help is appreciated.
You could use a positive lookahead (?=.{3,15}$ to check if the string has a length from 3 - 15 characters.
Because the minimum length of the string is 3 and has to start and end with a-zA-Z you can combine the 2 character classes in the middle in this case.
I think your pattern could be simplified by removing the repetition of the group due to the positive lookahead to:
^(?=.{3,15}$)[a-zA-Z]+[\\s'.a-zA-Z-]*[a-zA-Z]+$
Explanation
^ Start of the string
(?=.{3,15}$) Positive lookahead to assert the lenght 3-15
[a-zA-Z]+ Match 1+ times a lower/upper case char a-z
[\\s'.a-zA-Z-]* Charater class to match any of the listed 0+ times
[a-zA-Z]+ Match 1+ times a lower/upper case char a-z
$ End of the string
See the Java demo
Im trying to create a regex of numbers where 7 should appear atleast once and it shouldn't include 9
/[^9]//d+
I'm not sure how to do make it include 7 at least once
Also, it fails for the following example
123459, it accepts the string, even tho, there is a 9 included in there
However, if my string is 95, it rejects it, which is right
Code
Method 1
See regex in use here
(?=\d*7)(?!\d*9)\d+
Method 2
See regex in use here
\b(?=\d*7)[0-8]+\b
Note: This method uses fewer steps (170) as opposed to Method 1 with 406 steps.
Alternatively, you can also replace [0-8] with [^9\D] as seen here, which is basically saying don't match 9 or \D (any non-digit character).
You can also use \b(?=[^7\D]*7)[0-8]+\b as seen here, which brings the number of steps down from 170 to 147.
Method 3
See regex in use here
\b[0-8]*7[0-8]*\b
Note: This method uses few steps than both methods above at 139 steps. The only issue with this regex is that you need to identify valid characters in multiple locations in the pattern.
Results
Input
**VALID**
123456780
7
1237412
**INVALID**
9
12345680
1234567890
12341579
Output
Note: Shown below are strings that match.
123456780
7
1237412
Explanation
Method 1
(?=\d*7) Positive lookahead ensuring what follows is any digit any number of times, followed by 7 literally
(?!\d*9) Negative lookahead ensuring what follows is not any digit any number of times, followed by 9 literally
\d+ Any digit one or more times
Method 2
\b Assert the position as a word boundary
(?=\d*7) Positive lookahead ensuring what follows is any digit any number of times, followed by 7 literally
[0-8]+ Match any character present in the set 0-8
\b Assert the position as a word boundary
Method 3
\b Assert the position as a word boundary
[0-8]* Match any digit (except 9) any number of times
7 Match the digit 7 literally
[0-8]* Match any digit (except 9) any number of times
\b Assert the position as a word boundary
One way to do it would be to use several lookaheads:
(?=[^7]*7)(?!.*9)^\d+$
See a demo on regex101.com.
Note that you need to double escape the backslashes in Java, so that it becomes:
(?=[^7]*7)(?!.*9)^\\d+$
This has got a bit complex but it works for your use case :
(?=.*^[0-68-9]*7[0-68-9]*$)(?=^(?:(?!9).)*$).*$
First expression matches exactly one occurence of 7, accepts just numbers and second expression tests non-occurence of 9.
Try here : https://regex101.com/r/5OHgIr/1
If I find out correctly, you need a regex that accept all numbers that include at least one 7 and exclude 9. so try this:
(?:[0-8]*7[0-8]*)+
If you want found only numbers in a normal text add \s first and last of regex.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
so I'm trying to write a regular expression that can match the following scenarios.
12.1234 = match
112.12345678901 = wont match
1287729918192.123 = match
123927678281818.19883748383839 = won't match
23.1829aga = won't match
1722ahh.98822 = won't match
1.#$122 = won't match
Basically it should only match strings that contain no letters or special characters and values with less than 10 decimal places.
I'm really new to regular expression and am not sure how to accomplish this.
Thank you for any help!
Trick to writing simple regexes:
describe your pattern in words
look for quantifiers, character classes and/or other tokens that satisfy your description
combine all the tokens you found into one line!
Basically, your pattern can be described like this:
start of the string
1 to unlimited number of digits
a dot
between 1 and 10 digits
end of the string
We just translate the above descriptions into regex:
^
\d+
\.
\d{1,10}
$
And combine all these:
^\d+\.\d{1,10}$
This should work:
^\d*\.\d{1,10}\s
To deconstruct this a little bit:
"^" means starting at the start of the line of text
"\d*" means any number of digit characters (including zero!)
"." means the literal '.' character (we need to escape it with a '\' first)
"\d{1,10}" means between 1 and 10 amount of digits
"\s" means any type of whitespace
I would recommend you take a look at https://regexone.com/. An excellent resource that will help you understand how regex works, it helped me a lot!
Try this:
/^[0-9]*[.]{0,1}[0-9]{1,10}$/
The Explanation follows:
^ asserts position at start of the string
[0-9]* Match a single character in the range between 0 (index 48) and 9 (index 57) any times
[.]{0,1} Match a single character . at max one times
[0-9]{1,10} Matches between 1 and 10 times, a single character in the range between 0 (index 48) and 9 (index 57)
Pattern p = Pattern.compile("(?=[1-9][0-9]{2})[0-9]*[05]");
Matcher m = p.matcher("101");
while(m.find()){
System.out.println(m.start()+":"+ m.end()+ m.group());
}
Output------ >> 0:210
Please let me know why I am getting output of m.group() as 10 here.
As far as I understand m.group() should return nothing because [05] matches to nothing.
Your Pattern, (?=[1-9][0-9]{2})[0-9]*[05] consists of 2 parts:
(?=[1-9][0-9]{2})
and
[0-9]*[05]
The first part is a zero-width positive lookahead which searches for a number of length 3, and the first can not be 0. This matches your 101.
The second part searches for any amount of numbers and then a 0 or a 5. This matches the first 2 characters of 101, thus the result is 10.
See Java - Pattern for more information.
What your Regex is looking for is:
[1-9]:
match a single character present in the list below
1-9 a single character in the range between 1 and 9
[0-9]{2}:
match a single character present in the list below
Quantifier: {2} Exactly 2 times
0-9 a single character in the range between 0 and 9
[0-9]*:
match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
0-9 a single character in the range between 0 and 9
[05]:
match a single character present in the list below
05 a single character in the list 05 literally
for the String "101" this nacht the first 2 chars 101,
so you are printing.out:
System.out.println(**m.start()**+":"+ **m.end()**+ m.group());
where m.start() returns the start index of the previous match(char at
0). where m.end() returns the offset after the last character matched.
and where m.group() returns the input subsequence matched by the previous
match.
That regex was meant to match a number that's a multiple of 5 and greater than or equal to 100, but it's useless without anchors. It should be:
^(?=[1-9][0-9]{2}$)[0-9]*[05]$
The anchors ensure that both the lookahead and the main part are examining the whole of the string. But the task doesn't require a lookahead anyway; this works just fine:
^[1-9][0-9][05]$
As #AlanMoore states, there has to be an alignment.
Assertions are a self contained entity, all they have to do is Pass
to advance to the next construct.
Lets see what (?=[1-9][0-9]{2}) matches;
1111111110666
2222222222222222225666
33333333333333333333333330666
So far so good, on to the next construct.
Lets see what [0-9]*[05] matches.
What ever this matches is the final answer.
1111111110666
2222222222222222225666
33333333333333333333333330666
What to learn is that to get a cohesive answer, assertions have to be crafted to
coincide with constructs that come after them.
Here is an example of a constraint that could be applied
after the assertion.
The assertion state's it needs three digits and the first digit must be >= 1.
The constructs after the assertion state it can be any number of digit's,
as long as it ends with a 0 or 5.
This last part is distressing since it will match only the 500000
So for sure, you need at least three digits.
That can be done like this:
[0-9]{2,}[05]
This says two things
There must be at least three digits, but can be more
It must end with a 0 or 5.
That's it, put it all together, its:
(?=[1-9][0-9]{2})[0-9]{2,}[05]
Of course, this can be condensed to;
[1-9][0-9]+[05]
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 7 years ago.
I am parsing text and need to detect if any string contains any character NOT including A-Z, 0-9, full stop, comma, plus, minus, or any number of spaces.
I tried the regex expression: "[^A-Z0-9][^.][^,][^-][^+][\S+]"
as well as variations on this, which does not work correctly.
Examples of permissible strings:
1 23842U 96021A 15170.20596865 .00000124 00000-0 00000+0 0 9998
2 23842 0.0589 306.1344 0002868 147.0577 292.5546 1.00269795 70198
Invalid string:
1 2%8!2U 96021A 15170.20596865 .00000124 ^00000-0 00000+0 0 9998
Seems like you want to allow, spaces, alphabets, digits, dot, plus, minus.
Pattern p = Pattern.compile("^[A-Za-z,.+\\s\\d-]+$");