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

问题描述

需要另一个REGEX的帮助.我有一个扁平的txt字符串,需要在文件名后加上后缀.

例如,当我看到"SysParaI"时,我想抓住"002",等等.

到目前为止,我有,但是有些不对.有帮助吗???

Need help on another REGEX. I have a flat txt string and need to pull a suffix off of files names.

For example, When I see ''SysParaI'' I want to grab ''002.'' Etc.

So far I have, but somethings not right. Anyhelp????

Match match = Regex.Match(TextString,  @"(?<=\(SysPara\)[^)(]*\d+(?=\))"


05/25/12  06:34p                    79 SysParaI.002
05/25/12  06:34p                   437 EqtParaI.016
05/25/12  06:34p                  3143 EqtProgI.022

推荐答案

string extension = System.IO.Path.GetExtension(filename);


只要您可以输入文件名(而不是上面几行中的所有多余内容",这都将为您提供扩展名"002"或任何扩展名.

如果不能仅隔离文件名,为什么不只抓住每行的最后三个字符呢?这将使您很容易地进行扩展(除非该行不止于此).

要使用RegEx查找这些值,您需要执行以下操作:


This will give you just the "002" or whateve the extension is, as long as you can pass in the file name (and not all of the extra "stuff" in the above lines.

If you can''t isolate just the file name, why don''t you just grab the last three characters of each line? That would get you the extension quite easily (unless the line doesn''t end there).

To use RegEx to find these values, you would need to do something like this:

Match match = Regex.Match("05/25/12 06:34p 79 SysParaI.002", @"SysParaI.([0-9]{3})");
int fileNumber = Convert.ToInt32(match.Groups[1].Value);



这将在文件以"SysParaI"开头的小数点后找到三位数字.它将在您的字符串中的任何位置找到匹配项.仅当您在大量文本中查找文件而无法预测它们的位置时,才使用此选项.您可能需要修改RegEx以适应每个文件名(EqtParaI,EqtProgI等),但这应该很容易.



This would find the three digits after the decimal where the file started with "SysParaI". It would find matches anywhere in your strings. I would only use this if you were looking for files inside a lot of text and couldn''t predict where they would be. You would have to modify the RegEx to accomodate each file name (EqtParaI, EqtProgI, etc.) but this should be easy.



这篇关于REG表达问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 11:38