繁体   English   中英

使用C#从字符串中提取数字

[英]Extract Number from String using C#

我使用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
                         });

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

我需要在Grid视图中输出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();

        }

此正则表达式: £(?<from>.*?) to £(?<to>.*?)使用命名捕捉组fromto 通过其帮助,需要从description中提取值。

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

正则表达式测试

编辑:添加.Groups属性。

您可以使用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返回的结果的第一个位置和第二个位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM