正则表达式更改为句子大小写

发布于 2024-07-25 07:32:54 字数 374 浏览 7 评论 0原文

我正在使用 Notepad++ 在 5453 行语言文件中进行一些文本替换。 文件行的格式为:

variable.name = Variable Value Over Here, that''s for sure, Really

双撇号是有意的。

我需要将值转换为句子大小写,但单词“Here”和“Really”除外,它们是正确的并且应保持大写。 正如您所看到的,值中的大小写通常首先是混合的。

我已经为此工作了一段时间。 到目前为止我所得到的是:

 (. )([A-Z])(.+)

这似乎至少选择了正确的字符串。 替换件是我苦苦挣扎的地方。

I'm using Notepad++ to do some text replacement in a 5453-row language file. The format of the file's rows is:

variable.name = Variable Value Over Here, that''s for sure, Really

Double apostrophe is intentional.

I need to convert the value to sentence case, except for the words "Here" and "Really" which are proper and should remain capitalized. As you can see, the case within the value is typically mixed to begin with.

I've worked on this for a little while. All I've got so far is:

 (. )([A-Z])(.+)

which seems to at least select the proper strings. The replacement piece is where I'm struggling.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜心小果奶 2024-08-01 07:32:54
Find:    (. )([A-Z])(.+)
Replace: \1\U\2\L\3

在 Notepad++ 6.0 或更高版本中(内置 PCRE 支持)。

Find:    (. )([A-Z])(.+)
Replace: \1\U\2\L\3

In Notepad++ 6.0 or better (which comes with built-in PCRE support).

栖竹 2024-08-01 07:32:54

正则表达式替换无法对匹配项执行函数(如大写)。 您必须编写脚本,例如使用 PHP 或 JavaScript。

更新:请参阅乔纳斯的回答

我自己构建了一个名为 Text Utilities 的网页来执行此类操作事情:

  • 粘贴您的文本
  • 进入“查找,正则表达式和替换”(或按Ctrl+Shift+F
  • 输入您的正则表达式(我的将是 ^(.*?\=\s*\w)(.*)$)
  • 检查“^$ 匹配行限制”选项
  • 选择“将 JS 函数应用于匹配”
  • 添加参数(首先是匹配,然后是子模式),这里s,start,rest
  • 将return语句改为return start +rest.toLowerCase();

文本区的最终函数看起来像这样:

return function (s, start, rest) {
     return start + rest.toLowerCase();
};

也许添加一些代码来大写一些单词,例如“Really”和“Here”。

Regex replacement cannot execute function (like capitalization) on matches. You'd have to script that, e.g. in PHP or JavaScript.

Update: See Jonas' answer.

I built myself a Web page called Text Utilities to do that sort of things:

  • paste your text
  • go in "Find, regexp & replace" (or press Ctrl+Shift+F)
  • enter your regex (mine would be ^(.*?\=\s*\w)(.*)$)
  • check the "^$ match line limits" option
  • choose "Apply JS function to matches"
  • add arguments (first is the match, then sub patterns), here s, start, rest
  • change the return statement to return start + rest.toLowerCase();

The final function in the text area looks like this:

return function (s, start, rest) {
     return start + rest.toLowerCase();
};

Maybe add some code to capitalize some words like "Really" and "Here".

酒绊 2024-08-01 07:32:54

在 Notepad++ 中,您可以使用名为 PythonScript 的插件来完成这项工作。 如果您安装该插件,请创建一个新脚本,如下所示:

在此处输入图像描述

然后您可以使用以下脚本,替换您认为合适的正则表达式和函数变量:

import re

#change these
regex = r"[a-z]+sym"
function = str.upper

def perLine(line, num, total):
for match in re.finditer(regex, line):
    if match:
        s, e = match.start(), match.end()
        line = line[:s] + function(line[s:e]) + line[e:]
        editor.replaceWholeLine(num, line)

editor.forEachLine(perLine)

此特定示例的工作原理是查找特定行中的所有匹配项,然后对每个匹配项应用该函数。 如果您需要多行支持,Python 脚本“Conext-Help”解释了提供的所有函数,包括在“编辑器”对象下定义的 pymlsearch/pymlreplace 函数。

当您准备好运行脚本时,请先转到您希望脚本运行的文件,然后转到“脚本 >” 在 Python 脚本菜单中运行您的脚本。

注意:虽然如果您搞砸了,您可能可以使用记事本++的撤消功能,但最好先将文本放入另一个文件中以验证其是否有效。

PS 您可以使用 notepad++ 的内置查找对话框“查找”和“标记”正则表达式的每个出现位置,如果您可以选择所有这些,则可以使用 TextFX 的“字符 -> 大写”功能来解决此特定问题,但是我不知道如何从标记或找到的文本转到选定的文本。 但是,我想我会发布此内容以防万一有人这样做...

编辑:在 Notepad++ 6.0 或更高版本中,您可以使用“PCRE(Perl 兼容正则表达式)搜索/替换”(来源:http://sourceforge.net/apps/mediawiki/notepad-plus/?title= Regular_Expressions)因此可以使用像 (. )([Az])(.+) 这样的正则表达式和像 \1\U\2\ 这样的替换参数来解决这个问题3..

In Notepad++ you can use a plugin called PythonScript to do the job. If you install the plugin, create a new script like so:

enter image description here

Then you can use the following script, replacing the regex and function variables as you see fit:

import re

#change these
regex = r"[a-z]+sym"
function = str.upper

def perLine(line, num, total):
for match in re.finditer(regex, line):
    if match:
        s, e = match.start(), match.end()
        line = line[:s] + function(line[s:e]) + line[e:]
        editor.replaceWholeLine(num, line)

editor.forEachLine(perLine)

This particular example works by finding all the matches in a particular line, then applying the function each each match. If you need multiline support, the Python Script "Conext-Help" explains all the functions offered including pymlsearch/pymlreplace functions defined under the 'editor' object.

When you're ready to run your script, go to the file you want it to run on first, then go to "Scripts >" in the Python Script menu and run yours.

Note: while you will probably be able to use notepad++'s undo functionality if you mess up, it might be a good idea to put the text in another file first to verify it works.

P.S. You can 'find' and 'mark' every occurrence of a regular expression using notepad++'s built-in find dialog, and if you could select them all you could use TextFX's "Characters->UPPER CASE" functionality for this particular problem, but I'm not sure how to go from marked or found text to selected text. But, I thought I would post this in case anyone does...

Edit: In Notepad++ 6.0 or higher, you can use "PCRE (Perl Compatible Regular Expression) Search/Replace" (source: http://sourceforge.net/apps/mediawiki/notepad-plus/?title=Regular_Expressions) So this could have been solved using a regex like (. )([A-z])(.+) with a replacement argument like \1\U\2\3.

一桥轻雨一伞开 2024-08-01 07:32:54

提问者想到了一个非常具体的案例。
作为记事本++中一般的“更改句子大小写”
第一个正则表达式建议对我来说不能正常工作。
虽然不完美,但这是一个经过调整的版本
就我的目的而言,对原始版本来说是一个很大的改进:

find:    ([\.\r\n][ ]*)([A-Za-z\r])([^\.^\r^\n]+) 
replace: \1\U\2\L\3

您仍然有小写名词、名称、日期、国家等的问题,但是一个好的拼写检查器可以帮助解决这个问题。

The questioner had a very specific case in mind.
As a general "change to sentence case" in notepad++
the first regexp suggestion did not work properly for me.
while not perfect, here is a tweaked version which
was a big improvement on the original for my purposes :

find:    ([\.\r\n][ ]*)([A-Za-z\r])([^\.^\r^\n]+) 
replace: \1\U\2\L\3

You still have a problem with lower case nouns, names, dates, countries etc. but a good spellchecker can help with that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文