+ 4
Why is this regrex evaluating to false for non digits
(?=.*^//d)
32 ответов
+ 7
There's actually quite a lot wrong with this regex.
(?=.*^//d)
1. (?= ) is a positive lookahead which matches before the pattern, but does not necessarily capture the pattern itself.
2. The ^ is an anchor for beginning of the line. But I think you are wanting to use the not match character class which requires brackets: [^ ]
3. //d matches the literal text //d. I think what you want is the non numeric digits: [^/d] or \D
It would help if you provided test strings that should pass and should fail to understand what you are looking for.
I'm guessing you might want something like:
(.*[^\d]) or (.*\D)
Or... in Java: "(.*[^\\d])" or "(.*\\D)"
However, this pattern is a bit odd as I'm not really sure what you're matching on.
Share your positive and negative tests to match on and we can give better direction.
+ 4
George S Mulbah Cool 👌
BTW... Your original positive and negative tests didn't have special characters... That's why I didn't support them originally.
This is what I meant by coming up with a good list.
Anyway, I'm glad this was helpful.
Going to sleep now. Nearly 4am here. 😴
+ 2
David Carroll The regex should match any text but not a text that contain a whitespace in the middle or at the end
Example
George12= true
George@14=true
george@14=true
But
George 14 =false
George @ 14 =false
and
George@14\\s =false
I use the \\s to show a whitespace at the text end
also Danijel Ivanović pls help too
+ 2
[^\s]\d+[^\s]
[^] means does NOT contain, so this regex is saying match any digit not surrounded by white space
+ 2
Zeke Williams Ah... I see now. 😉👌
However, I'm still not sure what critical pieces are still missing from my regex based on the test samples.
George S Mulbah Take a look at this more test driven approach to the code.
https://code.sololearn.com/cLr1Y8nHo1Du/?ref=app
This should make everything much clearer for everyone. It should also help in applying adjustments with much clearer certainty of the impact on the test cases.
+ 1
George S Mulbah It's best if you give me a set of valid and invalid test strings.
Otherwise, we'll be chasing this down for a while. 🤷♂️
+ 1
Man... I keep getting blitzed with like storms which bury my other notifications.
Just now saw the test samples.
Assuming no spaces, at least one character, and must end with a number, the following pattern will satisfy the requirement:
^[^\s]*\d$
or for Java:
"^[^\\s]*\\dquot;
+ 1
Zeke Williams Thank a lot Sir you made it look easy
+ 1
I am now realizing that your test cases were not the strings 😂 I thought =false was part of the test, so let me edit my regex a little
+ 1
George S Mulbah I changed only one thing. I removed the * at the end of
]*$
So now it is just ]$
+ 1
One last thing George S Mulbah . If your goal is to make sure that there are no whitespaces in the string, and it doesn't matter where those whitespaces are, this is much better to use
"^[^\\s]+quot;
+ 1
Zeke Williams Either I've misunderstood the positive and negative tests or you're seeing it different. I'm good either way... 😉
That said, the reason I place the starting ^ and ending $ anchor tokens are for the following reasons:
1. My understanding is spaces aren't allowed anywhere. Without the start anchor, we would have some pass that should fail.
NOTE: Ignore the quotation marks. Added them to show the leading spaces.
Passes Correctly as Expected:
"asdf4"
Passes, but should Fail:
" asdf4"
"asd asd4"
" asd asd 4"
2. My understanding is the string must end with a number. Without the ending anchor, the following that should fail will pass:
"asdf4 "
"asdf4 asd"
"asdf4 asd "
Therefore, the start and end anchors are required.
But... I could be misunderstanding the requirements. 😉
+ 1
David Carroll twas I that misunderstood the requirements. As I corrected myself above, I thought that the '=true' and '=false' was part of the string being tested, but that's not the case. Therefore, you were correct in adding the start and end anchors, but the regex was still missing some critical pieces.
I am still unsure what the exact goal of the matching is for; no spaces, username followed by @ followed by numbers, no spaces near the numbers, something else? For now, I have offered a couple of those scenarios, and they seem to be working for the OP.
+ 1
George S Mulbah So... you are fine with a space as long it's the second position from the last?
Your pattern:
"^[^\\s]+.[A-z0-9//S$@#]quot;
Supports this value: "asdf4g 6"
I've updated my code with your pattern for you to test.
Just change line 35 to use Pattern_F to see the results.
+ 1
George S Mulbah Also... what's the purpose for [ ... //S ... ]? I'm trying to highlight the forward slash S in the character class with brackets.
+ 1
David Carroll that also none essential I remove it "^[^\\s]+[A-z0-9$@!#]quot;
+ 1
David Carroll OK thanks a lot Sir I am really learning a lot from you
+ 1
Syed Mudasir Rizvi you suppose to be asking this question in Q&A just click the notification icon. you see the message area it really that simple and pls stop asking your question on this thread
0
David Carroll Thank a lot That work for digit but I am actually trying to make sure this code does not match whitespace
https://code.sololearn.com/cpmTAfSFYrF4/?ref=app