搜索并替换为“仅匹配单词”选项

时间:2022-09-13 09:39:48

I need to replace all determiners from sentence. My problem that when i replace "a", it replace this letter in word.

我需要从句子中替换所有的决定者。我的问题是当我替换“a”时,它会用字替换这个字母。

str.stringByReplacingOccurrencesOfString("a", withString: "")

str.stringByReplacingOccurrencesOfString(“a”,withString:“”)

For example: a match a

例如:匹配a

The result will be: mtch

结果将是:mtch

But i want: match

但我想:匹配

2 个解决方案

#1


1  

You can detect whole words with \b word boundary, and also, you can grab optional whitespace after these words with \s* (1+ whitespace). Then, we can make sure no leading spaces are left with .stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).

您可以使用\ b字边界检测整个单词,并且还可以使用\ s *(1+空格)在这些单词后面获取可选的空格。然后,我们可以确保没有带有.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())的前导空格。

Also note that strings in Swift are C strings (support escape sequences) and since the regex special metacharacters need to be escaped with a literal backslash, you need to actually double the backslashes in the pattern (i.e. \s -> "\\s").

另请注意,Swift中的字符串是C字符串(支持转义序列),并且由于正则表达式特殊元字符需要使用文字反斜杠进行转义,因此您需要实际加倍模式中的反斜杠(即\ s - >“\\ s” )。

Here is Swift 2.1 sample code:

这是Swift 2.1示例代码:

var s = "a match a"
var out_s = ""
if let regex = try? NSRegularExpression(pattern: "\\ba\\b\\s*", options: .CaseInsensitive) {
    out_s = regex.stringByReplacingMatchesInString(s, options: .WithTransparentBounds, range: NSMakeRange(0, s.characters.count), withTemplate: "")
    print("\""+out_s.stringByTrimmingCharactersInSet(
        NSCharacterSet.whitespaceAndNewlineCharacterSet()
    )+"\"")
}

It prints "match".

它打印“匹配”。

Note also that options: .CaseInsensitive will perform a case insensitive match, use options: [] to make the search case sensitive.

另请注意,选项:.CaseInsensitive将执行不区分大小写的匹配,使用选项:[]使搜索区分大小写。

Now, if you have a list of the determiners, use alternation:

现在,如果您有一个确定者列表,请使用替换:

"\\b(?:an?|the)\\b\\s*"

This expression will match a, an or the followed with 0+ whitespace.

此表达式将匹配a,an或后跟0+空格。

#2


1  

You should try with word boundaries like this /\ba\b/. Should use g global search and case insensitive i flags.

您应该尝试像这样的单词边界/ \ ba \ b /。应该使用g全局搜索和不区分大小写的i标志。

Word boundary \b makes sure that there is no character followed or preceded by a.

字边界\ b确保没有字符后跟或前面有a。

Regex101 Demo

#1


1  

You can detect whole words with \b word boundary, and also, you can grab optional whitespace after these words with \s* (1+ whitespace). Then, we can make sure no leading spaces are left with .stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).

您可以使用\ b字边界检测整个单词,并且还可以使用\ s *(1+空格)在这些单词后面获取可选的空格。然后,我们可以确保没有带有.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())的前导空格。

Also note that strings in Swift are C strings (support escape sequences) and since the regex special metacharacters need to be escaped with a literal backslash, you need to actually double the backslashes in the pattern (i.e. \s -> "\\s").

另请注意,Swift中的字符串是C字符串(支持转义序列),并且由于正则表达式特殊元字符需要使用文字反斜杠进行转义,因此您需要实际加倍模式中的反斜杠(即\ s - >“\\ s” )。

Here is Swift 2.1 sample code:

这是Swift 2.1示例代码:

var s = "a match a"
var out_s = ""
if let regex = try? NSRegularExpression(pattern: "\\ba\\b\\s*", options: .CaseInsensitive) {
    out_s = regex.stringByReplacingMatchesInString(s, options: .WithTransparentBounds, range: NSMakeRange(0, s.characters.count), withTemplate: "")
    print("\""+out_s.stringByTrimmingCharactersInSet(
        NSCharacterSet.whitespaceAndNewlineCharacterSet()
    )+"\"")
}

It prints "match".

它打印“匹配”。

Note also that options: .CaseInsensitive will perform a case insensitive match, use options: [] to make the search case sensitive.

另请注意,选项:.CaseInsensitive将执行不区分大小写的匹配,使用选项:[]使搜索区分大小写。

Now, if you have a list of the determiners, use alternation:

现在,如果您有一个确定者列表,请使用替换:

"\\b(?:an?|the)\\b\\s*"

This expression will match a, an or the followed with 0+ whitespace.

此表达式将匹配a,an或后跟0+空格。

#2


1  

You should try with word boundaries like this /\ba\b/. Should use g global search and case insensitive i flags.

您应该尝试像这样的单词边界/ \ ba \ b /。应该使用g全局搜索和不区分大小写的i标志。

Word boundary \b makes sure that there is no character followed or preceded by a.

字边界\ b确保没有字符后跟或前面有a。

Regex101 Demo