English 中文(简体)
创建此方案常规表达式时需要帮助
原标题:Need Help in Creating a Regular Expression for this Scenario
  • 时间:2012-05-23 15:58:46
  •  标签:
  • c#
  • regex

我需要从遵循某种格式版式布局的文本行中分析一些信息。 这是文本文件看起来如何的一个例子 :

A. This is option a              C. This is option c
B. This is option b              D. This is option d

在一天结束时,我所想要的就是,在解析了以上两行之后, 我会在我的C#代码上写上:

string OptionA = "This is option a";
string OptionB = "This is option b";
string OptionC = "This is option c";
string OptionD = "This is option d";

A. 和 C. (或 B. 和 D.) 之间的空格可以是制表符( ),也可以是白空格的随机数。 当跳过代码和行时, 当读取代码和行时, 它的外观 :

"A.	This is option a	C. This is option c"

也可能是这个样子

"A.	This is option a        C. This is option c"

我或许需要一些帮助, 以“ ” 或一些白色空格取代“ C ” 来拆分这条线, 就如上述例子一样。

如能提供任何投入,将不胜感激。

最佳回答

下列正数应该这样做,

@"^([A-Z])[.](.+[^s])s+([A-Z])[.](.+)$"

每一行的哪一行

  • Groups[0] is the whole line
  • Groups[1] is the first letter (e.g. A)
  • Groups[2] is the first option (e.g. This is option a)
  • Groups[3] is the second letter (e.g. C)
  • Groups[4] is the second option(e.g. This is option c)
问题回答

给这人一个旋律:

[A-Z]..*((s){2,}|	)
[a-zA-Z][.]s[ws]*s

这将给您结果如 A。 这是一个选项 , 然后您可以剖析它来抓取选项字母和结果文本。 您还可能想要从 . Trim () 的行中删除尾端空白 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签