Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

使用正则匹配所有中文,但特定行除外

使用下面的正则表达式,可以匹配出所有的中文,但是我想把注释行排除,正则表达式应该怎样更改? 我知道排除//的表达式是(?!//),但是添加进去都没有效果,望高手指点
期望的输出结果:
文字一
文字二
文字三
文字四

$s= @'
class Program
{
    static void Main()
    {
        //这里的文字不处理
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        DocumentModel document = new DocumentModel();

        Section section = new Section(document);
        document.Sections.Add(section);

        Paragraph paragraph = new Paragraph(document);
        section.Blocks.Add(paragraph);

        Run run = new Run(document, "文字一"); 
        paragraph.文字二Inlines.Add(run);文字三

        文字四document.Save("test.docx");
    }
}
'@

[regex]::Matches($s, '[^x00-xff]+') | foreach { $_.value } | echo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

如果用 JS 的正则实现,大概是这样:
/(?<!//[^ ]*)[^x00-xff]+/g

result


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...