English 中文(简体)
Regex从文本文件中删除和弦
原标题:Regex to remove chords from a text file

我有一个大文件(大约2万行)的和弦音乐。

我正在尝试制作一个程序来从我的文件中删除和弦。

我查找和弦的正则表达式是:

Regex regex = new Regex("([A-G](#|b)*([a-z]|[0-9])*)(/[A-G](#|b)*([a-z]|[0-9])*)*");

这个问题是一个合乎逻辑的问题。

如何使用正则表达式检测文本中的一行是否只有和弦?

UPDATE: Sample of the file:

C                                     G 
Justo é o Senhor em seus santos caminhos, 
A -             G/B C     D4 D G 
Benigno em todas  as  suas  obras.  (bis) 
G          C          C7 
Perto está  o Senhor, (perto está dos que o invocam,) 
     F   C  D -   G  C               F        G         C F  
De todos que o invocam  (De todos que o invocam) 
C/G G   C                        F C 
Em   verdade. Aleluia! Aleluia! 
最佳回答

这个怎么样:

resultString = Regex.Replace(subjectString, @"^[A-Gd#bs/-]*$(?:
)?", "", RegexOptions.Multiline);

这将删除所有仅包含字母A到G、#b/以及-或空格的行。

问题回答

You can anchor your regex by wrapping it in ^...$.
This will force it to match entire lines.

然后,您可以循环通过File.ReadLines,如下所示:

var nonChords = File.ReadLines(path)
                    .Where(s => !regex.IsMatch(s));




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

热门标签