Retrieve number from string using Regex in c# [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.
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.

Related

How Can Get Characters Between 2 Character? [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 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.

Extract data from log file using Regex [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 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"));
}

How do I write a regular expression that matches strings with exactly one space between each word and no trailing or leading spaces [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.
Single regx for all these Conditions
1.Should allow only aphanumeric
2. along with only one space between words
3. Should allow only special characters like -.,'
4. Should not allow leading space, trailing space and consecutive blank space.
Valid input:
"testing with 2 regx solution"
Invalid input:
" testing with 2 regx solution" or "testing %^with 2 regx solution "
Try this
^(\w+\s)*\w+$
^ Start of string
( Start of group
\w+ Word of one or more characters
\s White space
) End of group
* Zero or more of the preeceding group
\w+ Word of one or more characters
$ End of string
inputString= Regex.Replace(inputString.Trim(),#"\s+"," ");
--SJ

Using substring in line C# [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'm having problems with the substring method, getting this error.
"Index and length must refer to a location within the string."
"Parameter name: length"
string[] nombre = item.Split(new char[]{' '});
this.listBox5.Items.Add(nombre[0].Substring(0,2).ToUpper()+nombre[1].Substring(0,1));
It means that the values you are passing to Substring are not valid for the string they are being called on. For example:
string s = "hello";
string x = s.Substring(0, 1); // <-- This is fine (returns "h")
string y = s.Substring(1, 3); // <-- Also fine (returns "ell")
string z = s.Substring(5, 3); // <-- Throws an exception because 5 is passed
// the end of 's' (which only has 5 characters)
As an aside, I see this a lot:
item.Split(new char[]{' '})
I think people are confused by the signature of the Split method. The following is sufficient:
item.Split(' ')

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