C#正则表达式 [英] C# regular expression

查看:92
本文介绍了C#正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



有人可以帮我写一个C#正则表达式从以下字符串 2.224 KB > @ {AnyText = 2.224 KB(2,277字节)



问候

塞巴斯蒂安

Hi everyone,

Can someone help me write a C# regular expression to extract "2.224 KB" from the following string @{AnyText=2.224 KB (2,277 bytes)

Regards
Sebastian

推荐答案

怎么样:

How about:
...
string input = ...;
Match match = Regex.Match(input, @"\w+\s*=\s*(\d+(?:\.\d+)?\s*[A-Z]+)");
string size = match.Success ? match.Groups[1].Value : string.Empty;
...



如果 @ 和<$ c $,则非常依赖输入字符串c> {与正确匹配文本相关。



注意:如果你需要文化意识,整个正则表达式变得更加困难:小数分隔符和千位分隔符在多种文化中可能相当奇怪(例如,一些斯堪的纳维亚语言,千分隔符是某种空间......)。在这种情况下,你需要更加模糊,例如 @\ w + \s * = \ * *(。*?\ s * [AZ] [AZ])



干杯

Andi


Depends very much on the input string if the @ and { are relevant to properly match the text.

Note: If you need to be culture aware, the whole regex becomes far more difficult: decimal separator and thousand separator may be rather "weird" in versious cultures (e.g. some scandinavian language, the thousand separator is some kind of space...). In that case, you need to be a bit more fuzzy, e.g. @"\w+\s*=\s*(.*?\s*[A-Z][A-Z])".

Cheers
Andi


需要更多信息才能正确回答。



假设:

1.字符串始终如下:@ {AnyText =< SizeInKBHere>(2,277字节)

2。这是总字符串而不是大字符串的一部分。



您可以从提供的字符串中获取子字符串。

1.获取索引=和第二个空格。

2.使用#1中的信息,从=后的子句字符串开始直到第二个空格。
More information is required before this can be answered appropriately.

Assumptions:
1. String always remains like: "@{AnyText=< SizeInKBHere > (2,277 bytes)"
2. This is the total string and not a part of a huge string.

You can get substring from the string provided.
1. Get the index of "=" and second space.
2. Using information from #1, get the substring starting after "=" till second space.


这篇关于C#正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆