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.
Related
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 9 years ago.
How can i get characters between two character ? Example ;
string example = "aaaaaaaaXbbbbXaaaaaa";
How can get bbbb?
You can use String.IndexOf and String.LastIndexOf methods of String class for getting the positions of X's in your string, after that you can use String.SubString method for based their positions.
string example = "aaaaaaaaXbbbbXaaaaaa";
int firstXposition = example.IndexOf("X");
int LastXposition = example.LastIndexOf("X");
Console.WriteLine(example.Substring(firstXposition + 1, LastXposition - firstXposition -1));
Output will be;
bbbb
Here is a DEMO.
You may try like this
where substring holds two parameter
first the starting point of the string after escaping the no of characters i.e 9
second is no of characters need to display i.e 4
string example = "aaaaaaaaXbbbbXaaaaaa";
string sub = input.Substring(9, 4);
Console.WriteLine("Substring: {0}", sub);
string s = "aaaaaaaaXbbbbXaaaaaa";
string[] words = s.Split('X');
now you can use the foreach loop to get whatever you want.
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...
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 have following logfile
10:12:28.571 INFO INFO McEvent (ExitWaitPostProcess) Stage: 2
10:12:28.571 INFO McReplay:StatusEvent: 0C0000004576656E74496E666F207631260000000A000000484C4D494E464F207631C0A80A0100007F0000010000010100000000000100000046658FDC5008000000303331333030303115000000
5761697420666F7220706F73742070726F636573735E00000045786974205761697420666F7220706F73742070726F63657373205374616765204E6F322C20592D617869732846726F6E742053696465292C202852656172204C616E65292C205043422049443A20202020202020202020202020202020
10:12:28.571 INFO McReplay:StatusEvent verbose: event:03130001, msg:'Wait for post process', submsg:'Exit Wait for post process Stage No2, Y-axis(Front Side), (Rear Lane), ID: '
10:12:28.571 INFO INFO McEvent (ExitWaitPostProcess) Stage: 2
How should my Regex look like to extract what is coming after McReplay till first line that begins with the time stamp.I have several occurrences in the file and I need all of them.
I thought about something similar to
new Regex(#"^(\d\d:\d\d:\d\d\.\d\d\d) INFO McReplay:(.*?)(\d\d:\d\d:\d\d\.\d\d\d)",
RegexOptions.Singleline | RegexOptions.Multiline);
Seems I have issue with this part (.*?)(\d\d:\d\d:\d\d\.\d\d\d)
What should I fix here ?
This will find all occurrences
List<string> results = new List<string>();
MatchCollection mc = Regex.Matches("yourstring", "McReplay:(.+?)[0-9]{2}:[0-9]{2}:[0-9]{2}", RegexOptions.Singleline);
foreach (Match item in mc)
{
results.Add(item.Result("$1"));
}
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 retrieve a number from a string wherever the number starts with 8,9 or 6 and length of the number should be 8 OR 9 Characters. E.g 92000000,9200 0000,9200-0000.
How about this: (?<!\d)([896]\d{3})(?:[-\s]?)(\d{4})(?!\d).
The (?:[-\s]?) eats the optional delimiters space or dash as a non-capturing group.
You get your number by concatenating the match groups 1 and 2:
var input = new string[] {
"81000000", "92000000", "9200 0000", "9200-0000"
};
var regex = new Regex (#"(?<!\d)([896]\d{3})(?:[-\s]?)(\d{4})(?!\d)");
foreach (var str in input) {
var match = regex.Match (str);
Console.WriteLine ("TEST: {0} {1} - {2}", str, match.Success,
match.Groups [1].Value + match.Groups [2].Value);
}
I have also tried (?<!\d)([896]\d{3}(?:[-\s]?)\d{4})(?!\d) and that won't remove the delimiter character from the match result.
Try the below regex:
(?<!\d)[896]\d{3}([- ]?)\d{4}(?!\d)
Try to solve using Regexpal from next time.
Cheers.
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.