RegEx:转换所有单词'首字母大写(Notepad ++或类似)

时间:2015-07-31 17:39:32

标签: regex string notepad++

我过去做了很多尝试来掌握正则表达式,我已经解决了问题,但仍然没有明确的理解。

目前,我也解决了一个问题,但解决方案太难看了。 所以,我有一些用空格分隔的引用字符串:

"string" "string two" "lots of strings"

我需要大写每个单词。因此,我为(.)查找并替换\u\1,为"(.)

找到第二次迭代"\u\1

问题是:

  • 如何在一次迭代中执行此操作(具体为:大写quote之后和space之后的所有字母)
  • 如何为一般的所有单词执行此操作
  • 如何对除预定义词列表之外的所有单词(如of等)
  • 执行此操作

和可选问题

  • 如何大写单词'第一个字母和小写其他。甚至更可怕:保留大写字母完整。 (我明白这太多了,但至少可能会有人愿意给我指导)

我已经在互联网上搜索并找到了部分答案,但它只是部分内容,我不知道如何将它们组合起来。还有其他一些例子,我也无法翻译它们:看起来它们是由一个字符串组成的替换,但在这里我有两个字段用于查找和替换。 请原谅我的无知。我为此感到羞耻,但好奇心占了上风。

3 个答案:

答案 0 :(得分:3)

以下是我的意思:

找到:\b(?!of|the)(\w)(\w+)
替换为:\u$1\E$2

"string" "string two" "lots of the strings"

变为:

"String" "String Two" "Lots of the Strings"

您可以在否定前瞻中添加所需的每个字词:(?!of|the|in|out)

答案 1 :(得分:1)

这在Perl中是可能的:

#!/usr/bin/perl

$test_data = '"string" "string two" "lots of strings"';
$test_data =~ s/\b(.)/\u$1/go;
print $test_data . "\n";

但我不确定它是否可以在Notepad ++中使用。

答案 2 :(得分:1)

I did not try this in Notepad++, (see below) but my Textpad editor (which is fairly similar to Notepad++) handles replacing \<(.) to \u$1 correctly. (Notice the different character sequence to identify word boundaries, \< for Textpad and \b for Perl.) After filling in rearch and replacement just check the "Regular Expression" checkbox, hit "Replace all" and thats it.

For the other goals, the regex libraries used in editors may not be powerful enough, because they support only a subset of the regex syntax. For lowercasing the rest of the word if its not all-uppercase, it would be easiest to use assertions, which are not supported by Textpad.

If you want to find out more about regular expressions, I would recommend to use one of the online regular expression testers, because they do not have the limitations of the editor's libraries, and have useful features like highlighting for matches and regular expression syntax.

Excluding word lists an stuff like that usually look rather ugly in regular expressions. Be sure to understand the idea behind regular expressions. They are engines which are very efficient at decision-making, but not at looking-up anything. But of course, a small word-list like (of|if|and|or|not|ping|pong) does not hurt anyone.

EDIT: ([" ])(.) and $1\u$2 work as well and are more close to your specification and do not convert I've to I'Ve. And I found out that Textpad regexes support look-ahead and look-behind assertions as well, as of Textpad 7.

EDIT:

they consist of one string that does substitution

Be aware of the Perl syntax for regular expressions, which looks like

$<variable> =~ s/<search>/<replacement>/<modifiers>

EDIT: Verfified that at least ([" ])(.) and $1\u$2 work in Notepad++ as well.

相关问题