怎么做这个正则表达式替换? [英] How to do this regex replace?

查看:102
本文介绍了怎么做这个正则表达式替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个编码中,我发现emdash endash和下划线,在两个单词之间,两个单词存储在列表中,但我的替换不起作用任何一个都有更好的解决方案。



  public   void  EnDashToHyphen(Office .IRibbonControl控制)
{

Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
MessageBox.Show( 已测试);

List< string> AcidList = new 列表< string>()
{
{ 一个},{ 两个}, { },{ 四个},{ 五个}
};
foreach(Word.StoryRanges中的Word.Range故事)
{
foreach(Word.Paragraph para in story.Paragraphs)
{
// Word。范围临时;
foreach(Word.Range w in para.Range.Words)
{
Word.Range d = w.Duplicate;

if(Regex。 IsMatch(w.Text,
[\\\2020 \ u2014 \ u005F] ,RegexOptions.Multiline))
{
if(AcidList.Contains(w.Previous(Word。 WdUnits.wdWord,1).Text)&& AcidList.Contains(w.Previous(Word.WdUnits.wdWord,1).Text))
{
Regex.Replace(d.ToString(),
^ [\ u2013 \ \\ u2014 \ u005F] $ ^ HYPHEN $ );
//MessageBox.Show(w.Text);
}
}
}
}
}
}

解决方案

^ HYPHEN


< blockquote> );
//MessageBox.Show(w.Text);
}
}
}
}
}
}


Regex isn' temdash endash和下划线,在两个单词之间 - 它是一行(或整个字符串)包含一个单一字符,必须是emdash,endash或下划线之一



我建议您首先尝试测试正则表达式:获取 Expresso 的副本[ ^ ] - 它是免费的,它检查并生成正则表达式。

是的,它会让你测试替换。


In this coding I found the emdash endash and underscore , between two words, that two words stored in list, but my replace not worked any one have better solution.

public void EnDashToHyphen(Office.IRibbonControl control)
        {
         
            Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
            MessageBox.Show("tested");

            List<string> AcidList = new List<string>()
            {
                 {"One"},{"Two"},{"The"},{"Four"},{"Five}        
       };
            foreach(Word.Range story in doc.StoryRanges)
            {
                foreach(Word.Paragraph para in story.Paragraphs)
                {
                    //Word.Range temp;
                    foreach(Word.Range w in para.Range.Words)
                    {
                        Word.Range d = w.Duplicate;
                       
                        if (Regex.IsMatch(w.Text, "[\u2013\u2014\u005F]", RegexOptions.Multiline))
                        {
                            if (AcidList.Contains(w.Previous(Word.WdUnits.wdWord,1).Text) && AcidList.Contains(w.Previous(Word.WdUnits.wdWord,1).Text))
                            {
                                Regex.Replace(d.ToString(), "^[\u2013\u2014\u005F]$", "^HYPHEN$");
                                //MessageBox.Show(w.Text);
                            }
                        }
                    }
                }
            }
        }

解决方案

", "^HYPHEN


"); //MessageBox.Show(w.Text); } } } } } }


That Regex isn't "emdash endash and underscore , between two words" - it's "a line (or whole string) containing one single character, that must be one of emdash, endash, or underscore"

I'd sugget that you try testing the regex first: Get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions.
And yes, it'll let you test Replace.


这篇关于怎么做这个正则表达式替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆