简体   繁体   English

正则表达式在 MS Word 中查找/替换文本不起作用

[英]Regex to find/replace text in MS Word not working

I have an output from a python script I wrote, which consists of portions of text extracted from a big dataset, after matching some pattern through regular expressions.我有一个我写的 python 脚本的输出,它包含从一个大数据集中提取的文本部分,在通过正则表达式匹配一些模式之后。

For the sake of visualization I would like to use MS Word to highlight such patterns within those portions of text, and I tried to use find/replace with the same regex I used in my script but without luck.为了可视化,我想使用 MS Word 在这些文本部分中突出显示此类模式,并且我尝试使用查找/替换与我在脚本中使用的相同正则表达式但没有运气。

EXAMPLE: I want to find every occurrence of the pattern work (?:\\w+ )?(?:\\w+ )? children示例:我想找到模式work (?:\\w+ )?(?:\\w+ )? children每个出现work (?:\\w+ )?(?:\\w+ )? children work (?:\\w+ )?(?:\\w+ )? children , ie allow at most any two other words between work and children , in the text work (?:\\w+ )?(?:\\w+ )? children ,即文本中最多允许在workchildren之间work任何两个其他词。

"[...] ability to work with children must be proven. Social Worker Job Essex Our Essex based client require permanent Social Workers to work in children and families teams. You should be a qualified Social Worker DIPSW, CQSW, CSS or equivalent [...]"

Running find/replace with the above regex to the above text don't return any match (wildcards allowed).使用上述正则表达式对上述文本运行查找/替换不会返回任何匹配项(允许使用通配符)。

Which regex should I use?我应该使用哪个正则表达式? Any help will be appreciated任何帮助将不胜感激

In Word, you would need to use two wildcard Finds:在 Word 中,您需要使用两个通配符查找:

Find = work <[!查找 = 工作 <[! ]@> children ]@> 孩子们

for cases with one intervening word, and对于有一个中间词的情况,和

Find = work <[!查找 = 工作 <[! ]@> <[! ]@> <[! ]@> children ]@> 孩子们

for cases with two intervening words.对于有两个中间词的情况。

As already noted by others, this is not possible in Microsoft Word's flavor of regular expressions.正如其他人已经指出的那样,这在 Microsoft Word 的正则表达式风格中是不可能的。 Instead, you should use standard regular expressions.相反,您应该使用标准的正则表达式。 It is actually possible to use standard regular expressions in MS Word if you use a special tool that integrates into Microsoft Word called Multiple Find & Replace (see http://www.translatortools.net/products/transtoolsplus/word-multiplefindreplace ).如果您使用集成到 Microsoft Word 中的称为 Multiple Find & Replace 的特殊工具,实际上可以在 MS Word 中使用标准正则表达式(请参阅http://www.translatortools.net/products/transtoolsplus/word-multiplefindreplace )。 This tool opens as a pane to the right of the document window and works just like the Advanced Find & Replace dialog.此工具作为文档窗口右侧的窗格打开,其工作方式与“高级查找和替换”对话框类似。 However, in addition to Word's existing search functionality, it can use the standard regular expressions syntax to search and replace any text within a Word document.但是,除了 Word 现有的搜索功能外,它还可以使用标准的正则表达式语法来搜索和替换 Word 文档中的任何文本。

In your particular case, your expression在您的特定情况下,您的表达

work (?:\\w+ )?(?:\\w+ )?children工作 (?:\\w+ )?(?:\\w+ )?children

will work fine.会正常工作。

Multiple Find & Replace is quite powerful: you can add any number of expressions (either using regular expressions or using Word's standard search syntax) to a list and then search the document for all of them, replace everything, display all matches in a list and replace only specific matches, and a few more things.多重查找和替换功能非常强大:您可以将任意数量的表达式(使用正则表达式或使用 Word 的标准搜索语法)添加到列表中,然后在文档中搜索所有这些表达式、替换所有内容、在列表中显示所有匹配项以及仅替换特定的匹配项,以及其他一些内容。

I created this tool for translators and editors, but it is great for any advanced search/replace operations in Word, and I am sure you will find it very useful.我为翻译人员和编辑人员创建了这个工具,但它非常适合 Word 中的任何高级搜索/替换操作,我相信您会发现它非常有用。

Best regards, Stanislav最好的问候,斯坦尼斯拉夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM