Exclude match in Regular Expression c# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to exclude a specific string when it is contained in an expression:
Example:
myurl.htm = exclude
myurl = include
I tried this one : ([a-z0-9]+)(?!.htm)
But looks like it doesn't work.

Try the following:
([a-z0-9]+)(?!^\.htm)
You had two errors in your expression:
You have to escape the dot . with a backslash because it means "matches any character (except newline)" unescaped.
You have to add a ^ to prevent cutting of the last character.
You can test your expression on this website: https://regex101.com/

Related

Regular expression generation for a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to create a regular expression to check the following string pattern, I tried following these tutorials but it's still confusing. Any help is appreciated.
Type: In Folder
(T or t)ype(spaces or no space):(spaces or no space)(i or I)n(spaces or multiple space)(f or F)older
So following your desired pattern at the end of your question:
(t|T)ype\s*:\s*(i|I)n\s*(f|F)older
The above pattern should match your string. Mind you, \s* is zero to unlimited spaces, which means it would match on the string you provided even if there were no spaces present; if there will always be at least one space present, you can replace them with \s+
Hope this helps!
(T|t)ype\s*:\s*(I|i)n\s*(f|F)older

Format a sting in based on Divider symbol [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
i have a string like this
4366|2d53|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|3f80|0|4248|295f|41c4|19c4|0|268
Here,
| - is denoted as a divider i want to make each divider consists exactly 4 charcters
Ex: 0|0 this must to be simplified as 0000|0000
12|13 this must to be simplified as 0012|0013
Note:
if the char. enclosed with | is less then 4 then add zero in front of the characters i.e. 12|13 is simplified as 0012|0013 not like this 1200|1300
Result:
4366|2d53|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|3f80|0|4248|295f|41c4|19c4|0|268
i want to convert this as
4366|2d53|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|0000|3f80|0000|4248|295f|41c4|19c4|0000|2680
That can be solved with simple string operations Split(), PadLeft() and Join()
string input = "4366|2d53|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|3f80|0|4248|295f|41c4|19c4|0|268";
string result = string.Join("|",input.Split('|').Select(x => x.PadLeft(4, '0')));
Something like this should work:
string.Join("|", myString.Split('|').Select(x => x.PadLeft(4,'0')));
I made a fiddle: https://dotnetfiddle.net/uK1kyr

c# RegEx, how to filter the resultset of regex [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a string “banaanapplebanaanappleapplebanaanappleappbanaanaapple”,
1. If I want to extract “apple”, the below resultset applys well.
RegEx : apple
result : "banaanapplebanaanappleapplebanaanappleappbanaanaapple”
If I want to extract “banaana” the below query works well.
RegEx : banaana
result :
“banaanapplebanaanappleapplebanaanappleappbanaanaapple”
I want to match “apple and banaana”
RegEx : banaana
result :
“banaanapplebanaanappleapplebanaanappleappbanaanaapple”
I want my resultset to contain only two matches of the apple..
How can I achieve this with regex?
You actually need an alternation operator with capturing group.
banaana|(apple)
The idea is, first banaana would greedily match all the banaana strings. | OR (apple) capture apple strings only from the remaining characters. So this won't overlap with banaana strings.
Example

Custom regular expression for string with number [closed]

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 6 years ago.
Improve this question
I have to validate a sting with regular expression.
String Should be in the NHCC-XXXXX-00 Format ,where X is a Number.
Correct strings:
NHCC-10010-00,
NHCC-78965-00,
NHCC-99654-00
Wrong strings:
NHCC-1001-00
NHCC-78965-0
NHC-99654-00
ASDF-99654-00
NHCC-F9654-00
NHCC-99654-01
Can any one help me to solve the above senario?
This should work:
"NHCC-\d{5}-00"
Demo
You need to use anchors inorder to do an exact match.
#"^NHCC-\d{5}-00$"
The regular expression you want is #"(?s)^NHCC-\d{5}-00$"
if (!Regex.IsMatch(input, #"(?s)^NHCC-\d{5}-00$"))
{
//not valid
}

One white space only [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need one white space only in my regular expressen.
How should I create the code that validate if one white space is availabe in the string value?
I'm not sure exactly what you mean but I'm guessing you want to check for exactly one whitespace, but any number of non-whitespace characters:
#"^\S*\s\S*$"
Example code:
Regex regex = new Regex(#"^\S*\s\S*$");
Console.WriteLine(regex.IsMatch("Hello, world!"));
Console.WriteLine(regex.IsMatch("This contains three spaces."));
Console.WriteLine(regex.IsMatch("Two\nlines."));
Output:
True
False
True
Other variations
To check if the string contains exactly one whitespace only (no other characters):
#"^\s$"
To check if the string contains at least one whitespace:
#"\s"

Categories