为什么这个行尾 (\\b) 在 stringr/ICU 和 Perl 中不被识别为字边界 [英] Why does is this end of line (\\b) not recognised as word boundary in stringr/ICU and Perl

查看:48
本文介绍了为什么这个行尾 (\\b) 在 stringr/ICU 和 Perl 中不被识别为字边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 stringr 我尝试检测字符串末尾的 符号,如下所示:

Using stringr i tried to detect a sign at the end of a string as follows:

str_detect("my text €", "€\\b") # FALSE

为什么这不起作用?它适用于以下情况:

Why is this not working? It is working in the following cases:

str_detect("my text a", "a\\b") # TRUE - letter instead of €
grepl("€\\b", "2009in €") # TRUE - base R solution

但它在 perl 模式下也失败:

But it also fails in perl mode:

grepl("€\\b", "2009in €", perl=TRUE) # FALSE

那么 €\\b-regex 有什么问题?正则表达式 €$ 适用于所有情况...

So what is wrong about the €\\b-regex? The regex €$ is working in all cases...

推荐答案

当您使用没有 perl=TRUE 的基本 R 正则表达式函数时,TRE 正则表达式风格 被使用.

When you use base R regex functions without perl=TRUE, TRE regex flavor is used.

似乎是TRE字边界:

  • 在非单词字符匹配字符串结尾位置后使用时,和
  • 在与字符串开头位置匹配的非单词字符之前使用时.

查看 R 测试:

> gsub("\\b\\)", "HERE", ") 2009in )")
[1] "HERE 2009in )"
> gsub("\\)\\b", "HERE", ") 2009in )")
[1] ") 2009in HERE"
> 

这不是 PCRE 和 ICU 正则表达式中 词边界的常见行为非单词字符之前的单词边界仅在字符前面带有单词字符时才匹配的风味,不包括字符串位置的开头(并且在非单词字符之后使用时,需要单词字符出现在单词边界之后):

This is not a common behavior of a word boundary in PCRE and ICU regex flavors where a word boundary before a non-word character only matches when the character is preceded with a word char, excluding the start of string position (and when used after a non-word character requires a word character to appear right after the word boundary):

有三个不同的位置可以作为单词边界:

- 在字符串的第一个字符之前,如果第一个字符是单词字符.
- 在字符串的最后一个字符之后,如果最后一个字符是单词字符.
- 在字符串中的两个字符之间,其中一个是单词字符,另一个不是单词字符.

There are three different positions that qualify as word boundaries:

- Before the first character in the string, if the first character is a word character.
- After the last character in the string, if the last character is a word character.
- Between two characters in the string, where one is a word character and the other is not a word character.

这篇关于为什么这个行尾 (\\b) 在 stringr/ICU 和 Perl 中不被识别为字边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆