How to include '-' in the string with regex check [duplicate] - c#

This question already has an answer here:
How to add hyphen to regex
(1 answer)
Closed 4 years ago.
I have the below regex, which currently allows '_' but not '-'.
string validCharacters = #"^\w+$";
Regex.Match(componentName, validCharacters);
But I want to also to include '-' as a valid character. The '-' can be at any place in the string. i,e: first,last,middle,after '_' etc.
Below are the few test cases:
"DTD_rtop-234"
"DTD-rtop_234"
"-DTD_rtop-234"
"DTD_rtop-234-"
"DTD_-rtop-234"
Any help will be much appreciated.

Combine the - character with the \w characters in a single character class:
[-\w].
Your desired regex is therefore:
^[-\w]+$.

Related

How to select whole text without special characters in single match? [duplicate]

This question already has answers here:
How do I remove all non alphanumeric characters from a string except dash?
(13 answers)
Closed 3 years ago.
I have some text, I need to select only alphanumeric characters only in single match.
I have tried this in regex
[^\W]+
Pattern : [^\W]+
Input: This is "My Page"
https://rubular.com/r/PMQwahJIqqiOOI
Output I Need: This is My Page
Remove everything that is not word character or space using this regex,
[^\w ]+
with empty space.
Regex Demo

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.

Regex - Minimum 6 chars and no whitespace. Everything else allowed [duplicate]

This question already has answers here:
Regular expression for not allowing spaces in the input field
(5 answers)
Closed 3 years ago.
I'm trying to use regex which checks only two things
Minimum 10 characters (No Max)
No whitespace allowed
I'm able to check minimum 10 chars with #"^[a-zA-Z0-9]{10,}$" and disallow white space with ^[^0-9 ]+$
Now the problem is, how to combine both of these and allow everything(alphanumeric including special characters) except white space
You could try to use a simpler regex pattern just to accept anything that is not a white-space: ^\S{10,}$
\S - matches any non-white-space character. More details here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference

Ignoring Apostrophe in Regex [duplicate]

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

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