1

这是工作的php代码

<?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;
}
 ?>

这就是我得到的

1|afaaf|racad|xarcd|User1
2|fsvgvdsg|wdafsc|aefvbdfg|User2
3|chthb|rtvfascf|xasedfvg|User3

C# 代码

    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";

我得到

<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

您可以看到我从 c# 代码中获得的字符串甚至与我的 php 结果都不接近。

如何从我的 php 代码中获取结果?

希望你明白我的问题是什么。

谢谢

4

3 回答 3

1

试试这个 :

change (.*) to (.*?) in you RegEx pattern

并将您的代码段修改为:

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";
于 2012-08-18T08:19:30.537 回答
0

使用正则表达式的替代方法是使用 LinqToXml,这里是一个示例。

方法

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
                                                    });

模型

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);
    }
}
于 2012-08-18T08:32:30.430 回答
0

试试这个。(测试

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";
        }
于 2012-08-18T08:33:23.357 回答