Having trouble creating a regular expression [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to check for a match in a string such that the string ends in a dash and then two digits. Therefore "bill-01" will be a match, as will "jared-43" but "josh" and "allen47-" won't. I just need a bool true/false to tell if a match was found for the given string.
Thank you.

this will match anything as long as it ends with - followed by two numbers
.*-[0-9]{2}
http://txt2re.com/
http://www.regexr.com/
http://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended
Have fun

string pattern = #".*-\d{2}";
Match match = Regex.Match(arg, pattern);
Console.Write("{0} ", arg);
if (match.Success)
{
Console.WriteLine("Matched");
}
else
{
Console.WriteLine("did NOT Match");
}

Related

Regex to remove c# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to remove some string from regex in c#.
I have a string
000603ABC140702-005051-I-FILL200-NNYYNY180319-142110-A2002.zip
From this I want to remove those strings
140702
142110
And I want a result as
000603ABC-005051-I-FILL200-NNYYNY180319--A2002.zip
How can I do that in regex?
You can use the Regex.Replace method (https://msdn.microsoft.com/en-us/library/xwewhkd1(v=vs.110).aspx).
var regex = new Regex("(140702)|(142110)");
var result = regex.Replace("000603ABC140702-005051-I-FILL200-NNYYNY180319-142110-A2002.zip", "");
Although, as already stated by #CompuChip, String.Replace would probably be more efficient in this case. There's a discussion of various methods of replacing substrings and their efficiency at https://blogs.msdn.microsoft.com/debuggingtoolbox/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance/.

Regex.Replace hashtags but ignore apostrophe ( [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have the following C# code to replace hashtags in HTML strings and it works good but it also replaces apostrophes since they encode as '
The code replaces the #39 with the URL.
Such as Hamburger's is now converted to Hamburger&<a href\"Default.aspx?search=39">39</a>s
How can I ignore apostrophes for the regex replace?
public String ReplaceHashTags(string strContent)
{
string strHashtags = #"#(\w+)";
strContent = Regex.Replace(strContent, strHashtags,
"$1");
return strContent;
}
You can tell the Regex to not match if the # is preceded by &:
var strHashtagPattern = #"(?<!&)#(\w+)";
If you want to exclude all possible special character escapes, a negative lookahead may be better:
var strHashtagPattern = #"(?!(?<=&)#[\w\d]+;)#(\w+)";

What regex pattern can I use to match this string? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
What regex pattern can I use to match the following:
Dxx-xxxx/xxx
So:
- Any string that starts with character 'D'
- Has any number of any character between the 'D' and the '-'
- Has any number of any character between the '-' and the '/'
- Has any number of any character after the '/'
Apologies if I haven't explained this very well!
What is so difficult on D.*-.*/.*?
If you want to ensure that there is at least one character for behind the D, - and / respectivly you should use .+ instead of those .*.

RegEX : Find match based on condition [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am new to RegEx and looking for a solution to this condition. I am writing my code in C# and don't want to use any STRING functions.
Below is a sample string that I get:
610WBDFGFGM0122544
Now the condition I need to check is,
If string starts with "6" && 6th character is "D"
Can anyone tell me how to write RegEx for the above condition?
You can use this regex:
^6.{4}D
Here, ^ is the start of the string, .{4} means any four characters.
Online demo

Replace substring which comes in middle of string only [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want regx pattern in C# which find substring in any string which comes in middle only. Let say ,
Input : "toprohitpop rohittoppop toppoprohit"
find substring : "rohit"
Replace with : "$$$$"
Output : "top$$$$pop rohittoppop toppoprohit"
if substring "rohit" comes in left or right of the string then it should not be replaced.Substring "rohit" will only be replaced when it comes in middle of string .
Thanks in advance.
Use non-word-break anchors:
\Brohit\B
The \B will only match if it is in the middle of a word.
Read about it.
var input = "toprohitpop rohittoppop toppoprohit";
var regex = new Regex(#"\Brohit\B");
var output = regex.Replace(input, "$$$$$$$$");
See "Anchors" in Regular Expression Language.
Also, be careful with the '$' in the substitution string (see comments)
Use the following regex: .+rohit.+
Basically it enforeces at least one char before rohit and one after

Categories