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

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

本文介绍了正则表达式更改为句子大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

双撇号是有意的.

我需要将值转换为句子大小写,除了"Here"和"Really"这两个词正确且应大写.如您所见,值中的大小写通常是混合在一起的.

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.

推荐答案

正则表达式替换无法在匹配项上执行功能(例如大写).您必须编写脚本,例如在PHP或JavaScript中.

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

更新:请参阅乔纳斯的答案.

我为自己建立了一个名为文本实用工具的网页这类事情:

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

  • 粘贴您的文本
  • 进入查找,正则表达式和替换"(或按 + + )
  • 输入您的正则表达式(我的是^(.*?\=\s*\w)(.*)$)
  • 选中"^ $匹配行数限制"选项
  • 选择将JS函数应用于匹配项"
  • 添加参数(首先是匹配项,然后是子模式),此处为s, start, rest
  • 将return语句更改为return start + rest.toLowerCase();
  • paste your text
  • go in "Find, regexp & replace" (or press ++)
  • 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();
};

也许添加一些代码以大写"Really"和"Here"之类的词.

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

这篇关于正则表达式更改为句子大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 10:53