【问题标题】:Extract Number from String using C#使用 C# 从字符串中提取数字
【发布时间】:2017-07-17 17:02:45
【问题描述】:

我有使用 XDocument.Load("Somelink"); 的 Rss 提要。我正在从服务器获取 XML 输出。

<item>
    <title>Senior Web Developer</title>
    <link>Some link</link>
    <guid isPermaLink="false">Some link</guid>
    <description><![CDATA[University of UK &lt;br /&gt;Salary: £39,324 to £46,924 pa]]></description>
</item>

在描述标签中我获取公司信息和薪水,我只需要该描述中的薪水部分,如何从该链接中提取薪水。

var items = (from x in xDoc.Descendants("item")
                         select new
                         {
                             title = x.Element("title").Value,
                             description = x.Element("description").Value
                         });

如何从描述标签中提取薪水。我想在两个不同的标签中显示该薪水。

我需要在网格视图 Salary from 和 Salary to 中输出。我尝试了 Regex.Match 方法,它只给出前两位数字。

代码:-

<asp:GridView  ID="gRss" runat="server" AutoGenerateColumns="false" 
     ShowHeader="false" CssClass="table table-bordered table-striped">
     <Columns>
         <asp:TemplateField>
             <ItemTemplate>
                 <table class="table table-bordered table-striped"> 
                     <tr>
                         <td class="info"><%#Eval("Title") %></td>
                     </tr>
                     <tr>
                         <td><%#Eval("SalaryFrom") %></td>
                     </tr>  <tr>
                         <td><%#Eval("SalaryTo") %></td>
                     </tr>
                 </table>
             </ItemTemplate>
         </asp:TemplateField>
     </Columns>
 </asp:GridView> 

C#代码

 List<Feeds> feeds = new List<Feeds>();
        try
        {
            XDocument xDoc = new XDocument();
            xDoc = XDocument.Load("Some link");
            var items = (from x in xDoc.Descendants("item")
                         select new
                         {
                             title = x.Element("title").Value,
                             description = x.Element("description").Value
                         });


            if(items != null)
            {
                foreach(var i in items)
                {

                  //  string resultString = Regex.Match(i.description, @"\d+").Value;
                    //resultString = Regex.Match(subjectString, @"\d+").Value;
                    var match = Regex.Match(i.description, @": £(?<from>.*?) to £(?<to>.*?) ");
                    var from = match["from"].Value;
                    var to = match["to"].Value;
                    Feeds f = new Feeds
                    {
                        Title = i.title,
                        Description = resultString
                    };
                    feeds.Add(f);
                }
            }
            gRss.DataSource = feeds;
            gRss.DataBind();

        }

【问题讨论】:

    标签: c# asp.net regex xml gridview


    【解决方案1】:

    这个正则表达式: £(?&lt;from&gt;.*?) to £(?&lt;to&gt;.*?) 使用命名的捕获组fromto。在它的帮助下,从description 中提取所需的值。

    var match = Regex.Match(description, @": £(?<from>.*?) to £(?<to>.*?) ");
    var from  = match.Groups["from"].Value;
    var to    = match.Groups["to"].Value;
    

    Regex Test

    编辑:添加了.Groups 属性。

    【讨论】:

    • 我无法使用 [] 将索引应用到 System.Text.regularExpression.Match Exception 类型的 epression 中,我在问题中添加了 C# 代码。
    • 那是因为它应该访问Match 对象的Groups 属性。像这样:match.Groups["from"].Value;
    【解决方案2】:

    您可以使用Regex.Matches提取工资范围。

    var matches1 = Regex.Matches(descriptionValue, "£([0-9,]+)");   
    for(int i=0; i < matches1.Count; i++)
         Console.WriteLine("Parameters >> " + i + "\t" + matches1[i].Value);
    

    在这里,您将获得Regex.Matches 返回的结果的从第一个位置到第二个位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      相关资源
      最近更新 更多