Answered by:
Regex match help...

Question
-
I'm trying to search a long string for a pattern I have the pattern but don't know how to set it up I have a general idea but not close.. in the code below were "number" is that means that it must be a number.. And put up to four matches, if it finds them in a string....never really played with Regex but from what I researched I think that it will do what I want , unless there is a better way?
var match = Regex.Match(String, "(number,number, number <?>");
foreach (match.Success)
{
string line1 = match;string line2 = match;
string line3 = match;
string line4 = match;
}
Thanks- Edited by superlurker Monday, September 29, 2014 7:14 AM
Monday, September 29, 2014 7:04 AM
Answers
-
It is the spaces that is the killer
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static string Bigstring = "</div></td></tr>\n" + "<tr><td width='25%'><h2 class='normal'>number 3</h2></td><td width='25%'><div class='normal'>2014-09-29</div></td>\n" + "<td><div class='normal'>\n" + "\n" + "4, 7, 9<br>\n" + "9098648</div></td></tr>\n" + "<tr><td width='25%'><h2 class='normal'>number 4</h2></td><td width='25%'><div class='normal'>2014-09-29</div></td>\n" + "<td><div class='normal'>"; static void Main(string[] args) { Match match = Regex.Match(Bigstring, @"(?'num1'\d+),\s*(?'num2'\d+),\s*(?'num3'\d+)\s*<br>", RegexOptions.Singleline); int num1 = int.Parse(match.Groups["num1"].Value); int num2 = int.Parse(match.Groups["num2"].Value); int num3 = int.Parse(match.Groups["num3"].Value); } } }
- Edited by Joel Engineer Tuesday, September 30, 2014 3:24 AM
- Marked as answer by superlurker Tuesday, September 30, 2014 7:30 PM
Tuesday, September 30, 2014 3:14 AM -
The code below works
string Bigstring = "1,2,3 <br>"; Match match = Regex.Match(Bigstring, @"\d+,\d+,\d+\s*<br>");
jdweng
- Marked as answer by superlurker Tuesday, September 30, 2014 7:31 PM
Monday, September 29, 2014 7:32 PM -
Here is the complete solution
string Bigstring = "1,2,3 <br>"; Match match = Regex.Match(Bigstring, @"(?'num1'\d+),(?'num2'\d+),(?'num3'\d+)\s*<br>"); int num1 = int.Parse(match.Groups["num1"].Value); int num2 = int.Parse(match.Groups["num2"].Value); int num3 = int.Parse(match.Groups["num3"].Value);
jdweng
- Marked as answer by superlurker Tuesday, September 30, 2014 7:30 PM
Monday, September 29, 2014 9:50 PM -
I don't recommend removing all spaces in general because there are some space in the string like "td width" that are needed. Here is the solution
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static string Bigstring = "</div></td></tr>\n" + "<tr><td width='25%'><h2 class='normal'>number 3</h2></td><td width='25%'><div class='normal'>2014-09-29</div></td>\n" + "<td><div class='normal'>\n" + "\n" + "4, 7, 9<br>\n" + "9098648</div></td></tr>\n" + "<tr><td width='25%'><h2 class='normal'>number 4</h2></td><td width='25%'><div class='normal'>2014-09-29</div></td>\n" + "<td><div class='normal'>"; static void Main(string[] args) { string Bigstring2 = Regex.Replace(Bigstring, @"\s", ""); Match match = Regex.Match(Bigstring2, @"(?'num1'\d+),(?'num2'\d+),(?'num3'\d+)<br>", RegexOptions.Singleline); int num1 = int.Parse(match.Groups["num1"].Value); int num2 = int.Parse(match.Groups["num2"].Value); int num3 = int.Parse(match.Groups["num3"].Value); } } }
jdweng
- Marked as answer by superlurker Tuesday, September 30, 2014 7:30 PM
Tuesday, September 30, 2014 3:35 AM -
Try this
string Bigstring2 = Regex.Replace(Bigstring, @"\s", ""); string pattern = @"((?'num'\d+),?)+<br>"; Match match = Regex.Match(Bigstring2, pattern, RegexOptions.Singleline | RegexOptions.RightToLeft); Group group = match.Groups["num"]; List<int> numArray = new List<int>(); for (int i = 0; i < group.Captures.Count ; i++) { string numStr = group.Captures[i].Value; numArray.Add(int.Parse(numStr)); }
jdweng
- Marked as answer by superlurker Tuesday, September 30, 2014 7:30 PM
Tuesday, September 30, 2014 2:26 PM -
The code works, it just getting the groups in reverse order. So I made a minor change
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static string Bigstring = "</div></td></tr>\n" + "<tr><td width='25%'><h2 class='normal'>number 3</h2></td><td width='25%'><div class='normal'>2014-09-29</div></td>\n" + "<td><div class='normal'>\n" + "\n" + "4, 7, 9<br>\n" + "9098648</div></td></tr>\n" + "<tr><td width='25%'><h2 class='normal'>number 4</h2></td><td width='25%'><div class='normal'>2014-09-29</div></td>\n" + "<td><div class='normal'>"; static void Main(string[] args) { string Bigstring2 = Regex.Replace(Bigstring, @"\s", ""); string pattern = @"((?'num'\d+),?)+<br>"; Match match = Regex.Match(Bigstring2, pattern, RegexOptions.Singleline | RegexOptions.RightToLeft); Group group = match.Groups["num"]; List<int> numArray = new List<int>(); for (int i = group.Captures.Count - 1; i >= 0; i--) { string numStr = group.Captures[i].Value; numArray.Add(int.Parse(numStr)); } } } }
jdweng
- Marked as answer by superlurker Tuesday, September 30, 2014 7:30 PM
Tuesday, September 30, 2014 4:30 PM
All replies
-
Hi,
your code is wrong, match.Success is bool, not a collection, try this code:
var match = Regex.Match(string, "(number,number, number <?>");
if (match.Success)
{
foreach(var item in match.Groups)
{
int a = int.Parse(item.ToString());
}
}
Please,set the answer as correct if it helps.
- Proposed as answer by Mr. Zator Monday, September 29, 2014 8:32 AM
Monday, September 29, 2014 8:08 AM -
Here is a code:
Regex myRegex = new Regex(@"\d+"); string strTargetString = @"45,12,34, 56"; foreach (Match myMatch in myRegex.Matches(strTargetString)) { if (myMatch.Success) { // do something } }
Noam B.
Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
Monday, September 29, 2014 10:40 AM -
The problem is the number will be different every time and only a single digit with comma and with the <?>
and I need to put the findings in different locations. I couldn't find much about setting up the matching for Regex..
Monday, September 29, 2014 12:44 PM -
The problem is the number will be different every time and only a single digit with comma and with the <?>
and I need to put the findings in different locations. I couldn't find much about setting up the matching for Regex..
Monday, September 29, 2014 12:45 PM -
Forgot to tell you that I'm using .net 3.5
Monday, September 29, 2014 3:18 PM -
Forgot to tell you that I'm using .net 3.5Monday, September 29, 2014 3:18 PM
-
Hi,
you can store all result in a list and then use it as you need
var match = Regex.Match(string, "(number,number, number <?>");
List<int> myList = new List<int>();
if (match.Success)
{
foreach(var item in match.Groups)
{
myList.add(int.Parse(item.ToString()));
}
}Please,set the answer as correct if it helps.
Monday, September 29, 2014 3:24 PM -
Shouldn't "number" be something like
@"^\d$"
Monday, September 29, 2014 4:00 PM -
The ^ is beginning of the line and $ is end of the line. You should use both at the same time. \d isn't always needed. If you know a number is located between parenthesis you can use "(.*)" which will match any character.
jdweng
Monday, September 29, 2014 4:30 PM -
I came up with this but, i forgot that I'm looking for a sub string match, though this is not going to cut it, but am I on the right track??
string Bigstring;
List<string> myList = new List<string>();
var match = Regex.Match(Bigstring, @"\d,\d,\d <br>");
if (match.Success)
{
foreach (var item in match.Groups)
{
myList.Add(int.Parse(item.ToString()));
}
}Monday, September 29, 2014 4:45 PM -
Yes, except you are only looking for one digit. Use \s for any white spaces
var match = Regex.Match(Bigstring, @"\d+,\d+,\d+\s*<br>");
- Edited by Joel Engineer Monday, September 29, 2014 5:03 PM
Monday, September 29, 2014 5:02 PM -
For some ungodly reason this is not working, I put the string in a textbox for testing, found the line were the code was and compared the to strings and there was still no match?? Would it be easier to use the textbox and loop through the lines, and check each line it's only searching for Ex. (could be a mix of numbers) "1, 2, 3 <br> " maybe a space some times , but if I put in a textbox the string is on one line... I'm stumped on this one...Monday, September 29, 2014 5:56 PM
-
The code below works
string Bigstring = "1,2,3 <br>"; Match match = Regex.Match(Bigstring, @"\d+,\d+,\d+\s*<br>");
jdweng
- Marked as answer by superlurker Tuesday, September 30, 2014 7:31 PM
Monday, September 29, 2014 7:32 PM -
In my testing this is as far as it gets ..
string Bigstring = "1,2,3 <br>";
string pattern = @"\d\s*<br>";
Match match = Regex.Match(Bigstring, pattern);for some odd reason it won't take the commas???
the code above will work, but it's just the back end?? I don't get it...
Monday, September 29, 2014 7:53 PM -
Here is the complete solution
string Bigstring = "1,2,3 <br>"; Match match = Regex.Match(Bigstring, @"(?'num1'\d+),(?'num2'\d+),(?'num3'\d+)\s*<br>"); int num1 = int.Parse(match.Groups["num1"].Value); int num2 = int.Parse(match.Groups["num2"].Value); int num3 = int.Parse(match.Groups["num3"].Value);
jdweng
- Marked as answer by superlurker Tuesday, September 30, 2014 7:30 PM
Monday, September 29, 2014 9:50 PM -
I'm running .net 3.5 and have using System.Text.RegularExpressions; at the top ... I don't know why it's not matching, but I will show you were I'm getting data from..
byte[] store = client.DownloadData(WebLocation);
string Bigstring = System.Text.Encoding.UTF8.GetString(store);Is it because I'm looking for a substring??
Tuesday, September 30, 2014 12:15 AM -
Put a break point after the Bigstring instruction. Then hover over Bigstring and click on down arrow. Then open Text Visualizer and copy/paste results into the "Insert Code Block" so none of the control characters get changed. I will look at the results.
jdweng
Tuesday, September 30, 2014 2:33 AM -
Here it is, it looks pretty clean cut to me? if all fails is there a way to loop threw a textbox.line[num] and check each line for a string match?
</div></td></tr> <tr><td width='25%'><h2 class='normal'>number 3</h2></td><td width='25%'><div class='normal'>2014-09-29</div></td> <td><div class='normal'> 4, 7, 9<br> 9098648</div></td></tr> <tr><td width='25%'><h2 class='normal'>number 4</h2></td><td width='25%'><div class='normal'>2014-09-29</div></td> <td><div class='normal'>
Tuesday, September 30, 2014 3:02 AM -
It is the spaces that is the killer
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static string Bigstring = "</div></td></tr>\n" + "<tr><td width='25%'><h2 class='normal'>number 3</h2></td><td width='25%'><div class='normal'>2014-09-29</div></td>\n" + "<td><div class='normal'>\n" + "\n" + "4, 7, 9<br>\n" + "9098648</div></td></tr>\n" + "<tr><td width='25%'><h2 class='normal'>number 4</h2></td><td width='25%'><div class='normal'>2014-09-29</div></td>\n" + "<td><div class='normal'>"; static void Main(string[] args) { Match match = Regex.Match(Bigstring, @"(?'num1'\d+),\s*(?'num2'\d+),\s*(?'num3'\d+)\s*<br>", RegexOptions.Singleline); int num1 = int.Parse(match.Groups["num1"].Value); int num2 = int.Parse(match.Groups["num2"].Value); int num3 = int.Parse(match.Groups["num3"].Value); } } }
- Edited by Joel Engineer Tuesday, September 30, 2014 3:24 AM
- Marked as answer by superlurker Tuesday, September 30, 2014 7:30 PM
Tuesday, September 30, 2014 3:14 AM -
Ok then, can i just remove all spaces with Regex then search... How would you do that with Regex?
I had though that was the case, when I put it in a text box I had lines to work with.. less space but don't know how to get match like that..
- Edited by superlurker Tuesday, September 30, 2014 3:26 AM
Tuesday, September 30, 2014 3:24 AM -
I don't recommend removing all spaces in general because there are some space in the string like "td width" that are needed. Here is the solution
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static string Bigstring = "</div></td></tr>\n" + "<tr><td width='25%'><h2 class='normal'>number 3</h2></td><td width='25%'><div class='normal'>2014-09-29</div></td>\n" + "<td><div class='normal'>\n" + "\n" + "4, 7, 9<br>\n" + "9098648</div></td></tr>\n" + "<tr><td width='25%'><h2 class='normal'>number 4</h2></td><td width='25%'><div class='normal'>2014-09-29</div></td>\n" + "<td><div class='normal'>"; static void Main(string[] args) { string Bigstring2 = Regex.Replace(Bigstring, @"\s", ""); Match match = Regex.Match(Bigstring2, @"(?'num1'\d+),(?'num2'\d+),(?'num3'\d+)<br>", RegexOptions.Singleline); int num1 = int.Parse(match.Groups["num1"].Value); int num2 = int.Parse(match.Groups["num2"].Value); int num3 = int.Parse(match.Groups["num3"].Value); } } }
jdweng
- Marked as answer by superlurker Tuesday, September 30, 2014 7:30 PM
Tuesday, September 30, 2014 3:35 AM -
After lots of testing I got this, to work..but there is a small problem I know that there are two match in the string, but the code stops on the first match ??
List<string> myList = new List<string>();
string Data2 = Regex.Replace(Data, @"\s", "");
Match match = Regex.Match(Data2, @"\d,\d,\d\s*<br>", RegexOptions.Singleline);
if (match.Success)
{
foreach (var item in match.Groups)
{
myList.Add(item.ToString());
}
}the code above was giving me two digits then comma, so i borrowed some of your code... it finds the match.. just not all of them???
Tuesday, September 30, 2014 4:05 AM -
Solved that last question, working hard LOL, but there is one small problem I need it not to match if there is a comma in the front of the first string?? Because it pick up 2 extra numbers that just had more commas, whats the command for that??
List<string> myList = new List<string>();
string pattern = @"\d,\d,\d\s*<br>";
string Data2 = Regex.Replace(Data, @"\s", "");
foreach (Match m in Regex.Matches(Data2, pattern, RegexOptions.Singleline))
{
myList.Add(m.ToString());
}- Edited by superlurker Tuesday, September 30, 2014 4:44 AM
Tuesday, September 30, 2014 4:43 AM -
Did you look at my solution where I said spaces where a killer. Look at the '\s' in the pattern.
Match match = Regex.Match(Bigstring, @"(?'num1'\d+),\s*(?'num2'\d+),\s*(?'num3'\d+)\s*<br>", RegexOptions.Singleline);
jdweng
Tuesday, September 30, 2014 8:47 AM -
That works... but what's happening now is "1,2,3 <br>" and "0,9,1,2 <br>";
it's returning "1,2,3 <br>" and "9,1,2 <br>" because of the comma in the front is there any way to filter that, so it only matches with out the comma in the front of the set of three?
- Edited by superlurker Tuesday, September 30, 2014 1:46 PM
Tuesday, September 30, 2014 1:45 PM -
You have two choices
1) Add a fourth item to the pattern. When you have only 3 items the fourth will be empty.
2) Use pattern below
@"((?'num'\d+),?)+\s*<br>"
See webpage below for RegEx language
http://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx
jdweng
Tuesday, September 30, 2014 2:26 PM -
Try this
string Bigstring2 = Regex.Replace(Bigstring, @"\s", ""); string pattern = @"((?'num'\d+),?)+<br>"; Match match = Regex.Match(Bigstring2, pattern, RegexOptions.Singleline | RegexOptions.RightToLeft); Group group = match.Groups["num"]; List<int> numArray = new List<int>(); for (int i = 0; i < group.Captures.Count ; i++) { string numStr = group.Captures[i].Value; numArray.Add(int.Parse(numStr)); }
jdweng
- Marked as answer by superlurker Tuesday, September 30, 2014 7:30 PM
Tuesday, September 30, 2014 2:26 PM -
That grabbed the wrong strings.... I just need to tell Regex on the first match don't have a comma in front of it? then everything will be perfect..Tuesday, September 30, 2014 2:37 PM
-
The code works, it just getting the groups in reverse order. So I made a minor change
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static string Bigstring = "</div></td></tr>\n" + "<tr><td width='25%'><h2 class='normal'>number 3</h2></td><td width='25%'><div class='normal'>2014-09-29</div></td>\n" + "<td><div class='normal'>\n" + "\n" + "4, 7, 9<br>\n" + "9098648</div></td></tr>\n" + "<tr><td width='25%'><h2 class='normal'>number 4</h2></td><td width='25%'><div class='normal'>2014-09-29</div></td>\n" + "<td><div class='normal'>"; static void Main(string[] args) { string Bigstring2 = Regex.Replace(Bigstring, @"\s", ""); string pattern = @"((?'num'\d+),?)+<br>"; Match match = Regex.Match(Bigstring2, pattern, RegexOptions.Singleline | RegexOptions.RightToLeft); Group group = match.Groups["num"]; List<int> numArray = new List<int>(); for (int i = group.Captures.Count - 1; i >= 0; i--) { string numStr = group.Captures[i].Value; numArray.Add(int.Parse(numStr)); } } } }
jdweng
- Marked as answer by superlurker Tuesday, September 30, 2014 7:30 PM
Tuesday, September 30, 2014 4:30 PM -
When I run this , it only gives me 2 numbers..Tuesday, September 30, 2014 5:22 PM
-
Are you sure it isn't working? The code as posted give three values. What two numbers are you getting?
jdweng
Tuesday, September 30, 2014 7:09 PM -
The first two..Maybe it's just may computer..LOL the best that I could come up with was this....
List<string> myList = new List<string>();
string pattern = @",?\d,\d,\d\s*<br>";
string Data2 = Regex.Replace(Data, @"\s", "");
foreach (Match m in Regex.Matches(Data2, pattern, RegexOptions.Singleline))
{
string t = m.ToString();
if ("," != t.Substring(0, 1))
myList.Add(t);
}Regex is not user friendly... the code was working but Regex doesn't have enough filters to catch what I need, so I made my own, it was giving the right string and part of another the "comma" so I just did a substring catch, to filter it. This will do it..
I want to thank you, for not giving up on me, as you were, trying, so was I, most people would have given up on me..LOL , But the main problem that you caught that made the difference was too many spaces.. Regex needs to be rewritten..
Once again thanks, for your help..
Tuesday, September 30, 2014 7:30 PM