This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
I was trying to understand the following regex
^(?!\s)(?!.*\s\s)[ A-Za-z0-9'-]{1,35}(?<!\s)$
Here I understood,
^(?!\s) - (lookahead) means will not match if there is a leading space.
[ A-Za-z0-9'-]{1,35} - will allow A to Z, a to z, 0 to 9, (space), ' and -.
(?<!\s)$ - means will not match if there is a trailing space.
I am unable to understand what this sub-expression is doing?
(?!.*\s\s)
(?!.*\s\s)
means that in the string there should not be 2 adjacent spaces.
.* will scan the string.
\s\s will look for adjacent spaces.?! is negative lookahead so it will break when it finds such a string.See demo.
https://regex101.com/r/eB8xU8/4
I think there is no effect for this lookup.. It will return always true because here you are using greedy search.
It should be
(?!.*?\s\s): which means no two spaces continuous..
.* this is for match everything situation with out looking here and there
.* ? this makes this as a lazy operator and consider situation written at the right side of .*?
Related
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 5 months ago.
I am absolutely clueless when it comes to Regex strings. I am trying to create a custom validator on a model using [RegularExpression("myValidator")] How can I create a regex expression to validate the following formats
######-##
######-#
where # is a number. Could someone help me out?
Thanks!
\d means digit.
{N} means previous symbol repeated N times
so, basically you want:
\d{6}-\d{2}
which would match 6 digits, a dash, and 2 more digits.
You can also do:
\d{6}-\d{1,2}
which would match 6 digits, a dash, and then 1 or 2 more digits, and therefore work for either format you described.
This question already has answers here:
How do I match an entire string with a regex?
(8 answers)
RegularExpression Validation attribute not working correctly
(2 answers)
Closed 3 years ago.
I am using this exact regex in a RegularExpressionValidator and it works fine. However this code in c# is returning true. Can you please let me know what I am doing wrong?
I am checking for numbers only regex
var x = Regex.IsMatch("1234asdf", #"[0-9]+").ToString();
Because is finding what you're looking for inside the provided string.
You have tyo use ^ and $ to force the whole string to mathc the regex:
^[0-9]+$
^ matches the beginning, $, the end of the string
Please, look ath the Anchors section of this doc: Regular Expression Language - Quick Reference
BTW, instead of [0-9] you should use \d
Other answers talk about why your regex needs to be anchored. However, nobody's picked up on the RegularExpressionValidator part of your question.
I'm assuming you mean System.Web.RegularExpressionValidator. RegularExpressionValidator does more than just checking whether the regex matches - it also checks whether the regex is an exact match. That is, it effectively adds the ^ and $ anchoring for you.
The code:
Match m = RegexUtil.Match(controlValue, ValidationExpression, RegexOptions.None, MatchTimeout);
return(m.Success && m.Index == 0 && m.Length == controlValue.Length);
Note how it checks m.Success, but also checks that the match starts at the beginning of the value being checked, and ends at the end of the value being checked.
(System.ComponentModel.DataAnnotations.RegularExpressionAttribute does the same thing).
Because you trying to match one or more digit from 0 to 9. Explaininh:
[0-9] //Match digit 0-9 you can also use /d-(Match any digit char)
+ //Match one or more of the preceding token
If you have some digit in you string, this code:
var x = Regex.IsMatch("1234asdf", #"[0-9]+").ToString();
will always return True.
For exaple if you try this one:
var x = Regex.IsMatch("asdf", #"[0-9]+").ToString();
will return False
You have to use ^ and $ to force the whole string to match the regex pattern
^[0-9]+$
^ matches the beginning, $ the end of the string
In C# Write the expression like below format:
var x = Regex.IsMatch("1234", #"^[0-9]+$").ToString();
This question already has answers here:
Regex how to match an optional character
(5 answers)
Closed 5 years ago.
I have a simple Regex that looks for the words "won't connect" in a string.
(.*) won't connect.*
But I want to have it so it can ignore the apostrophe in case the user types in "wont connect".
(.*)won'?t connect.*
should be sufficient, matches " ' " 0 or 1 times
check the demo here
Your pattern:
(.*) won't connect.*
The * quantifier is greedy by default. That means your regex will go wrong with:
foo won't connect won't connect
See demonstration.
If you add a question-mark behind the quantifier, it will be lazy, meaning it will match only as much for the pattern to succeed. See an explanation here.
Improved pattern:
(.*?) won'?t connect.*
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
I am new to regex. What does regex expression match pattern "\[.*\]" mean?
If I have a text like "Hello [Here]", then success is returned in the match. And match contain [Here].
I read that:
. indicates Any except \n (newline),
* indicates 0 or more times
I don't understand the "\". It believe it is just escape sequence for "\".
So, is the expression "\[.*\]" trying to match a pattern like \[Any text\]?
Yes, you are right. It will match any characters enclosed in []. The .* imply any or no characters enclosed in [].
Also you should try this link which is a very helpful regex tool. You can input the regex pattern and check for matches easily.
I have tried this on regexr, here is a screen shot:
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I want to code a regular expression pattern that needs to check the following:
Every dash (-) character must be immediately preceded and followed by a letter or number; consecutive dashes are not permitted.
How to do this?
^(?!(?:.*-(?=$|-))|-).*$
Try this.See demo.
https://regex101.com/r/aZ6zX0/4
This uses negative lookahead to make sure - is not followed by - or $ and does not start with -.
We only know every dash should be surrounded with letters or digits, but we have no clue if the whole string is limited to letters, digits and dashes. Also it was not specified if string is required to contain dashes at all.
Assuming ##$abc-def#% is a valid string, we need to go with something like:
#"^[^-]+?(?:(?<=[\da-z])-(?=[\da-z])[^-]+?)*$"
DEMO
If only letters, digits and dashes are allowed, it is even simpler:
#"^[\da-z]+(?:-[\da-z]+)*$"
DEMO
You could use the below simple regex which uses negative lookahead assertion.
^(?!-|.*-$|.*-[\W_]|.*[\W_]-).*$
DEMO
.*-[\W_]|.*[\W_]- part in the negative lookahead assertion ensures that the - isn't preceded nor followed by a non-word character or _ symbol.