Reputation: 67
Here is working php code
<?php
$str = '<users>
<user id="1" value1="afaaf" value2="racad" value3="xarcd">User1</user>
<user id="2" value1="fsvgvdsg" value2="wdafsc" value3="aefvbdfg">User2</user>
<user id="3" value1="chthb" value2="rtvfascf" value3="xasedfvg">User3</user>
</users>';
$out = '';
if(preg_match_all('|<user id="(.*)" value1="(.*)" value2="(.*)" value3="(.*)">(.*)</user>|',$str,$matches))
{
for($i=0;$i<count($matches[1]);$i++)
$out .= $matches[1][$i].'|'.$matches[2][$i].'|'.$matches[3][$i].'|'.$matches[4][$i].'|'.$matches[5][$i]."\n";
echo $out;
}
?>
This is what I get
1|afaaf|racad|xarcd|User1
2|fsvgvdsg|wdafsc|aefvbdfg|User2
3|chthb|rtvfascf|xasedfvg|User3
C# code
string patt = "<user id=\"(.*)\" value1=\"(.*)\" value2=\"(.*)\" value3=\"(.*)\">(.*)</user>";
string str = "<users><user id=\"1\" value1=\"afaaf\" value2=\"racad\" value3=\"xarcd\">User1</user><user id=\"2\" value1=\"fsvgvdsg\" value2=\"wdafsc\" value3=\"aefvbdfg\">User2</user><user id=\"3\" value1=\"chthb\" value2=\"rtvfascf\" value3=\"xasedfvg\">User3</user></users>";
MatchCollection mc;
Regex r = new Regex(patt);
mc = r.Matches(str);
for (int i = 0; i < mc.Count; i++)
textBox1.Text += mc[i].Value + "\r\n";
and I get
<user id=\"1\" value1=\"afaaf\" value2=\"racad\" value3=\"xarcd\">User1</user><user id=\"2\" value1=\"fsvgvdsg\" value2=\"wdafsc\" value3=\"aefvbdfg\">User2</user><user id=\"3\" value1=\"chthb\" value2=\"rtvfascf\" value3=\"xasedfvg\">User3</user>\r\n
You can see that string what i get from c# code isn't even close to my php result.
How to get result like from my php code?
Hope you understand what is my problem here.
Thanks
Upvotes: 1
Views: 177
Reputation: 1204
try this.(TESTED)
string patt = "<user id=\"(?<id>.*?)\" value1=\"(?<value1>.*?)\" value2=\"(?<value2>.*?)\" value3=\"(?<value3>.*?)\">(?<name>.*?)</user>";
string str = "<users><user id=\"1\" value1=\"afaaf\" value2=\"racad\" value3=\"xarcd\">User1</user><user id=\"2\" value1=\"fsvgvdsg\" value2=\"wdafsc\" value3=\"aefvbdfg\">User2</user><user id=\"3\" value1=\"chthb\" value2=\"rtvfascf\" value3=\"xasedfvg\">User3</user></users>";
MatchCollection mc;
Regex r = new Regex(patt);
mc = r.Matches(str);
foreach (Match item in mc)
{
string id = item.Groups["id"].Value;
string value1 = item.Groups["value1"].Value;
string value2 = item.Groups["value2"].Value;
string value3 = item.Groups["value3"].Value;
string name = item.Groups["name"].Value;
textBox1.Text += id + "|" + value1 + "|" + value2 + "|" + value3 + "|" + name + "\r\n";
}
Upvotes: 0
Reputation: 16812
An alternative to using regular expressions could be to use LinqToXml, here is an example.
Method
const string xmlString = "<users><user id='1' value1='afaaf' value2='racad' value3='xarcd'>User1</user><user id='2' value1='fsvgvdsg' value2='wdafsc' value3='aefvbdfg'>User2</user><user id='3' value1='chthb' value2='rtvfascf' value3='xasedfvg'>User3</user></users>";
var doc = XDocument.Parse(xmlString);
// defensive coding should be applied, this example
// omits it for simplicity
// the u will be a list of type User (3 items in this example)
var u = doc.Descendants("user").Select(user => new User
{
Id = int.Parse(user.Attribute("id").Value),
Value1 = user.Attribute("value1").Value,
Value2 = user.Attribute("value2").Value,
Value3 = user.Attribute("value3").Value
});
Model
public class User
{
public int Id { get; set; }
public string Value1 { get; set; }
public string Value2 { get; set; }
public string Value3 { get; set; }
public override string ToString()
{
return string.Format("{0}|{1}|{2}|{3}", Id, Value1, Value2, Value3);
}
}
Upvotes: 0
Reputation: 2750
try this :
change (.*) to (.*?) in you RegEx pattern
and modify your snippet as:
for (int i = 0; i < mc.Count; i++)
textBox1.Text += mc[i].Groups[1].Value + mc[i].Groups[2].Value + mc[i].Groups[3].Value + + mc[i].Groups[4].Value + mc[i].Groups[5].Value + "\r\n";
Upvotes: 1