Grep 用于 .PO 文件,或 RegEx 用于分隔符之间的文本块
从这个文件开始:
msgid "a string"
msgstr ""
msgid ""
"A longer string wraps "
"on multiple lines."
msgstr ""
Grep RegEx 来替换所有 msgstr
行,如下所示:
msgid "a string"
msgstr "{a string}"
msgid ""
"A longer string wraps "
"on multiple lines."
msgstr ""
"{A longer string wraps "
"on multiple lines.}"
在我无限的天真中,我会搜索
\bmsgid "\b(*)\b"
msgstr "\b
... 将其替换为
msgid "\1"
msgstr "{\1}"
但这不会让我有任何结果。在网上搜索并尝试针对 Java 或 C# 调整各种解决方案后,我放弃并决定在这里询问。
您在上面看到的文件语法适用于 .PO 翻译文件。
我为此使用文本编辑器,Mac OS 上的 TextWrangler,它支持 Grep RegEx 语法。
谢谢。
Start with this file:
msgid "a string"
msgstr ""
msgid ""
"A longer string wraps "
"on multiple lines."
msgstr ""
Grep RegEx to replace all msgstr
lines like this:
msgid "a string"
msgstr "{a string}"
msgid ""
"A longer string wraps "
"on multiple lines."
msgstr ""
"{A longer string wraps "
"on multiple lines.}"
In my infinite naiveté, I would search for
\bmsgid "\b(*)\b"
msgstr "\b
...to replace it with
msgid "\1"
msgstr "{\1}"
But this doesn't get me anywhere. After searching the web and trying to adapt various solutions for Java or C#, I gave up and decided to ask here.
The file syntax you see above if for .PO translations files.
I am using a text editor for this, TextWrangler on Mac OS, which supports Grep RegEx syntax.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那里有两种不同的模式。
msgid "a string"
的第一个模式似乎表明msgid
后面的总数据是单行模式。 ``msgid ""` 的第二种模式表示数据位于多行中。由于它们是两种不同的模式,如果您试图将它们全部匹配到一个正则表达式中,您将会陷入困境。 (除非您使用 Perl 或除 RegEx 之外还具有某些程序逻辑的东西...)您说您想使用 Text Wrangler,所以我将限制我的评论,以了解其中的作用。
我认为您需要使用两个保存的模式执行两个查找/替换循环,才能在 Text Wrangler (TW) 中轻松完成此操作。在 TW 中,您可以在模式开头使用
(?sm)
标志,以使.*
模式匹配行结尾。 TW 保存了以前的 Grep 匹配,因此一旦您获得两个有效的查找/替换模式,这就相当容易了。因此,第一行模式可以与您拥有的
msgid "([^"]*)"
模式匹配。第二个模式可以在 BB Edit 或 TW 中使用(?sm) 进行匹配msgid ""[^"]*(.*)msgstr ""
您可能可以编写一个带有更改和命名捕获的正则表达式来匹配这两种模式并一次性替换,但这会花费更少的时间用 Perl 快速写一些东西...
There are two different patterns there. The first pattern of
msgid "a string"
seems to indicate that the total data followingmsgid
is a one line pattern. The second pattern of ``msgid ""` indicates that the data follows in multiple lines.Since they are two different patterns, and you will tie yourself in knots trying to match them all in one regex. (Unless you use Perl or something with some program logic in addition to the RegEx...) You said you wanted to use Text Wrangler so I will limit my comments to what works in that.
I think you will need to do two find / replace cycles with two saved patterns to do this easily in Text Wrangler (TW). In TW you can use the
(?sm)
flag at the beginning of your pattern to have.*
patterns match line endings. TW saves the previous Grep matches so it is fairly easy once you get your two find / replace patterns that work.So the one line pattern can be matched with the
msgid "([^"]*)"
pattern you have. The second pattern can be matched in BB Edit or TW with(?sm)msgid ""[^"]*(.*)msgstr ""
You potentially could write a regex with alteration and named captures to match the two patterns and replace in one go, but it would take a lot less time to write something quick in Perl...