RegEx preceded dash [duplicate] - c#

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.

Related

Why is this regex expression returning 'true'? [duplicate]

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();

C# Regex to find section number ,with no special character after it [duplicate]

This question already has answers here:
Regex whitespace word boundary
(3 answers)
Closed 4 years ago.
Regex -> ^([\s]*(?i)(section)[\s]*(?=[xvi])M*D?C{0,4}L?x{0,4}v{0,4}i{0,4})[\s]?
Text -> Section VII, some text.....
This regex matches Section VII and Section VII(space).
But it should not match "Section VII," , as after the match there is one special character.
What you need is a zero-width negative lookahead assertion:
^([\s]*(?i)(section)[\s]*(?=[xvi])M*D?C{0,4}L?x{0,4}v{0,4}i{0,4})(?!\S)
The critical part is (?!\S), which means that no (!) non-whitespace character (\S) should be at the end. It's a zero-width expression, which means that the character itself (if any) won't be a part of your match.

What does regex expression match pattern "\\[.*\\]" mean? [duplicate]

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:

Trying to understand the regex. Need Help in understanding the regex [duplicate]

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 .*?

How do I write a regex to match a string that doesn't contain a word? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular expression to match string not containing a word?
To not match a set of characters I would use e.g. [^\"\\r\\n]*
Now I want to not match a fixed character set, e.g. "|="
In other words, I want to match: ( not ", not \r, not \n, and not |= ).
EDIT: I am trying to modify the regex for parsing data separated with delimiters. The single-delimiter solution I got form a CSV parser, but now I want to expand it to include multi-character delimiters. I do not think lookaheads will work, because I want to consume, not just assert and discard, the matching characters.
I figured it out, it should be: ((?![\"\\r\\n]|[|][=]).)*
The full regex, modified from the CSV parser link in the original post, will be: ((?<field>((?![\"\\r\\n]|[|][=]).)*)|\"(?<field>([^\"]|\"\")*)\")([|][=]|(?<rowbreak>\\r\\n|\\n|$))
This will match any amount of characters of ( not ", not \r, not \n, and not |= ), or a quoted string, followed by ( "|=" or end of line )

Categories

Resources