English 中文(简体)
在C#定期表述中适当利用特征小组
原标题:Using Character Groups properly in a C# regular expression
  • 时间:2012-01-13 04:01:18
  •  标签:
  • c#
  • regex

更好的办法是将这一雷格斯简化为一种更可怕的形式,但我似乎无法适当实施特征小组,以供再利用。 关于如何更好地实现这一匹配的任何其他建议都将得到赞赏。

加入:

<Formatting Type="B">any text</Formatting>

这可以在其他格式的标签中加以说明。

<Formatting Type="B"><Formatting Type="I">any text</Formatting>any text</Formatting>

下面的雷克斯就是trick,但似乎比应该更加复杂,因为我三次重复这一节。

最终目标是以标准的超文本标签取代<B> <I> <U>等所有条目。

[40w!?:.]*

Overall Regex is the following

<Formatting Type="[BIU]{1}">([40w!?:.]*(<[BIU]>)*[40w!?:.]*(</[BIU]>)*[40w!?:.]*)*</Formatting>
问题回答

我认为,这是你试图做到的:

<Formatting Type="([BIU])">([ w!?:.]*(?:</?[BIU]>[ w!?:.]*)*)</Formatting>

没有必要为开放和关闭超文本标签单独生产,比你需要区分<代码><B>、<I><U>。 重要的是,在您配对开标之后,您不消费任何more在截止日期之前的开标;/Formatting>。 如果原始标签被正确地封顶,那么超文本标签也将是这样。

我假定只有三种类型的格式,在案文中就没有其他的角逐或类似的东西。 既然如此,你就不需要与监管机构那样严格限制。

text = Regex.Replace(text,
    @"<Formatting Type=""([BIU])"">([^<]*(?:</?[BIU]>[^<]*)*)</Formatting>",
    @"<$1>$2</$1>");

当然,你需要从案文上多发通行证,以确保你能够更换所有标签。 根据你的样本案文:

<Formatting Type="B"><Formatting Type="I">any text</Formatting>any text</Formatting>

......在第一次通行证之后,将改为:

<Formatting Type="B"><I>any text</I>any text</Formatting>

......并在第二次通行证之后:

<B><I>any text</I>any text</B>

我认为,你会发现这一点非常困难,特别是因为格式标签可以相互连接。

您可能希望避免陷入疯狂状态,因为https://stackoverflow.com/questions/1732348/regex-match- open-tags-gh-xhtml-sol-tags/1732454#1732454“显然,StackOverflow用户是

答案表明,可以通过使用“平衡匹配”。

也许你会更不用试图利用XML技术来达到这一目的(或许是XSLT),而不要reg。





相关问题
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. ...

热门标签