How to form this Regex [closed] - c#

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Suppose the string is:
string item = "t-ewrwerwerwerwer\r-rr\wrjkwlr";
I want to Replace all - except when it is preceded by r.
So resut will be
string cleanItem = "tewrwerwerwerwer\r-rr\wrjkwlr"'
What regular expression can be used?

I think this regular expression is a little more efficient:
-(?<!r-)
Or if your language doesn’t support negative look-behind assertions, use this expression:
(^|[^r])-
and replace it by \1 (first matching group).

A replacement on (?<!r)- by an empty string should do the trick I think.

(?<!r)-
As long as your regex flavor supports zero-width look-behind, that is.

Related

Regular expression comparison [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I want to check whether my string variable contain the particular regular expression pattern or not
xxx-xx-x
(x is numerical value) using c#. If it contains then I need to return true or false.
Can anyone please help me to resolve this issue..
Use the returned value by Regex.IsMatch().
Regex regex = new Regex("[0-9]{3}-[0-9]{2}-[0-9]");
bool containsPattern = regex.IsMatch(stringToVerify);
This is the regex you are looking for
\b\d{3}-\d{2}-\d\b
\b is a boundary..If you don't use it,you would also match 111-22-345 or 111-22-3-33 which i guess you don't want to match
using System.Text.RegularExpressions;
public static bool ControlRegex(string input)
{
Match match = Regex.Match(input, #"([A-Za-z0-9\-]+)",RegexOptions.IgnoreCase);
if (match.Success)
{
return true;
}
}
You can try something like this... You have to put correct regular expression to secend parametre of Regex.Match...
You can find the correct regex with a regex program. For example "RegEx TestBed" you can download it from here; http://regextestbed.codeplex.com/releases/view/60833 with this program, you put your text in text area, and in pattern area you try to find correct regex. And below, in the the list area, and program shows you the matches according your regex, so you can try and find your correct regex...

Need regex for digits and dashes [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I need a regex for this format xxxx-xxx-xx.jpg where x is digits [0-9].
To match for example: 3402-560-27.jpg
This regex is what you want:
\d{4}-\d{3}-\d{2}\.jpg
\d represents digits so \d{4} mean 4 digits.. in regex a . matches any single character so to match a literal . it needs to be escaped with a \.
This is one of the simplest regexes to write: put \d for each digit, - for each dash, and a \. for each dot. Letters correspond to themselves, so jpg goes in unchanged.
When you have more time, you can earn some "points for style" by learning about the explicit quantifier notation for repeated groups.

Converting C# line to VB.net [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I was trying to convert the following c# code to vb.net.
I see the problem is my lack of familiarity with the syntax of the parameters of OrderByDescending() What is the proper VB.Net equivalent of the C# line?
//C# code
SelectedFolder.Search("ALL", true).OrderByDescending(_ => _.Date).ToList();
//VB.Net part which doesn't work
For Each msg In SelectedFolder.Search("ALL", True).OrderByDescending(Function(_).[Date]).ToList()
After removing the underscore before [Date] the error became,
Error 1 Identifier expected.
The _ character is a line continuation in VB. Try changing the variable name to something more common, like x
For Each msg In SelectedFolder.Search("ALL", True).OrderByDescending(Function(x) x.[Date]).ToList()

what does format {0:x} mean? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I came across this C# literal and was wondering what does it mean?
Especially, in the following case:
string.Format("{0:x}", byteArray[i]);
Thanks
It means format the first argument (index 0) as hexadecimal: http://msdn.microsoft.com/en-us/library/s8s7t687(v=vs.80).aspx
It means the first argument will be output as hexadecimal (in lowercase !!).
To output uppercase you could use "{0:X}".
Look msdn for more info about string formatting : MSDN Custom string format
This represents the hexadecimal format.

What is the .net regex to find text between '$' and '<'? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I want to find the regex pattern to find the text between a string and a char and replace spaces in the text with _.
Example. < Node Type="Text">Event Log < /Node >
Expected output : Event_Log
Thanks in advance. Please help.
string s = "here is my text $$$ Hello World </stop>";
Match m = Regex.Match(s, "(\\$[^<]*)<");
if (m.Success)
{
Console.WriteLine(m.Groups[1].Value);
}
string str = "$$$ Hello World </stop>";
string sPattern = "[\\$]{3}([\\d\\s\\w]*)</stop>";
Match m = Regex.Match(str, sPattern, RegexOptions.IgnoreCase);
if (m.Success) {
Console.WriteLine(m.Groups(1));
}
Converted from VB code and not tested after but should be ok.
Assuming the example is correct and the text of your question wrong, you need:
\$+[^$<]*(?=<)
If it's the other way around, try this:
(?<=\$+)[^$<]*<
BTW, all questions like this can be more easily answered using a tool like this online regex tester.

Categories