簡體   English   中英

R中str_detect函數中的單詞邊界

[英]Word boundaries in str_detect function in R

在下面的字符串中,僅當在字符串中找到字符“ AD”時,我才希望str_detect返回TRUE。

ocode<-"ADV TXN CODE SCHED CC AMEX"

我試過了

str_detect(ocode,pattern="AD") which returns TRUE as expected
str_detect(ocode,pattern="ADV") which also returns TRUE as expected
str_detect(ocode,pattern="AD\b") returns FALSE as expected 

str_detect(ocode,pattern="ADV\b") returns FALSE

我不明白為什么會這樣? 是否不應該找到“ ADV”后跟空格並返回true?

我要解決的問題是過濾給定輸入的字符串,但是如果搜索條件是AD,則過濾器只返回帶有AD和ADV的字符串,而我希望過濾器僅返回帶有AD的字符串。

嘗試使用str_detect(ocode,pattern="ADV\\\\b") 在R中使用正則表達式時,必須轉義\\

該正則表達式只返回\\\\b之前的內容,因為它返回非單詞字符之前的所有內容:

str_extract(ocode,pattern="ADV\\b")
## [1] "ADV"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM