English 中文(简体)
利用Regex与所引述的外衣、非替罪引语相匹配
原标题:Using Regex to match quoted string with embedded, non-escaped quotes

我正试图将以下模式中的扼杀与ex相匹配。

string text = " Emma , The Last Leaf , Gulliver s travels ";
string pattern = @" (.*?) ,?";

foreach (Match match in Regex.Matches(text,pattern,RegexOptions.IgnoreCase))
 {
    Console.WriteLine(match + " " + match.Index);
    Console.WriteLine(match.Groups[1].Captures[0]);
 }

这与“Emma”和“最后印页”相匹配,但第三种相匹配是“Gulliver”。 但所期望的配对是“肝脏旅行”。 我怎么能为这种模式 build?

最佳回答

由于<代码>,是你的划界人,你可以尝试改变你的做法。 它应当发挥作用。

string pattern = @" (.*?) (?:,|$)"; 

这一工作的方式是,它只看一字,然后是 com或线末。

问题回答

我认为,这可以起到<条码>的作用(*?) ,,作为定期表述。

您可考虑采用以下办法:

 "(?<=^ | , ).*?(?= $| , )"

http://www.ohchr.org。

kent$  echo " Emma , The Last Leaf , Gulliver s travels "|grep -Po "(?<=^ | , ).*?(?= $| , )"
Emma
The Last Leaf
Gulliver s travels

如果你有单一报价,可以打字。 Gulliver s 载有单一、令人不安的替罪羊,没有办法将其与扼杀的结尾区别开来。 您可以总是通过调子和三点(<>条码>)从双方分离,但我并不相信你想要的东西:

string text = " Emma , The Last Leaf , Gulliver s travels ";

foreach(string s in text.split(new char[] { , })) {
    Console.WriteLine(s.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. ...

热门标签