阅读人数从字符串在C#字符串、人数

2023-09-06 08:33:28 作者:原来你也爱她

我要什么?

我要显示我的页面上的天气信息。    我想结果显示在浏览器中特定的文化。

I want to display weather information on my page. I want to display the result in the browser specific culture.

我在做什么?

我使用MSN RSS用于这一目的。    MSN返回XML格式的报表。我解析XML并显示结果。

I use MSN RSS for this purpose. MSN returns the report in XML format. I parse the XML and display results.

有什么问题,我会面对?

在显示的报告,我要解析的XML节点,<数据> 这将是在不同的文化不同的价值观

When displaying the report, I have to parse an XML node, <data> which will be different values in different culture.

有关如,

EN-US:罗:46°F您好:67°F可能有precipitation:20%

去DE:Niedrig:46°F赫希斯特:67°F Niederschlag%:20%

我想只读低,precipitation价值高的机会。即,我想读的46,67和20%。

I want to read only low, high and chance of precipitation values. i.e., I want to read 46, 67 and 20%.

有人可以请给我一个解决方案吗?

Can somebody please give me a solution for this?

可能RegX或someother方法也是罚款与我: - )

May be RegX or someother method is also fine with me :-)

在此先感谢!

推荐答案

如果你只想要数字,你可以使用常规的EX pression,例如以下内容:

If you only want the numbers, you can use a regular expression, for example the following:

(\d+).*?(\d+).*?(\d+%)

在PowerShell中快速测试显示,它至少为您的输入数据:

A quick test in PowerShell shows that it does work at least for your input data:

PS Home:\> function test ($re) {
>>   $a -match $re; $Matches
>>   $b -match $re; $Matches
>> }
>>
PS Home:\> $a = "Lo: 46°F. Hi: 67°F. Chance of precipitation: 20%"
PS Home:\> $b = "Niedrig: 46°F. Höchst: 67°F. Niederschlag %: 20%"
PS Home:\> test "(\d+).*?(\d+).*?(\d+%)"
True

Name                           Value
----                           -----
3                              20%
2                              67
1                              46
0                              46°F. Hi: 67°F. Chance of precipitation: 20%
True
3                              20%
2                              67
1                              46
0                              46°F. Höchst: 67°F. Niederschlag %: 20%

不过,它不会工作了,如果任何语言环境可能在描述字符串中使用的数字。

However, it won't work anymore if any locale might use numbers in the description strings.

您可以添加其他约束条件,比如要求一个冒号之前每场比赛:

You can add other constraints, like requiring a colon before every match:

: (\d+).*?: (\d+).*?: (\d+%)

这应该处理虚假的数字串中的其它地方。但是,最好的方式整体实际上是从,让你的数据机读源获取数据,而不是供人食用

This should deal with spurious numbers elsewhere in the string. But the best way overall would actually be to get your data from a source which gives you the data for machine reading, not for human consumption