简体   繁体   English

正则表达式中单词之间的 n 个字符

[英]n characters between words in regex

I would like to match following pattern using regex (in R, version 3.3.1): word, anything up to 10 characters - word.我想使用正则表达式(在 R,版本 3.3.1 中)匹配以下模式:单词,最多 10 个字符的任何内容 - 单词。

I have tried the following code: "word1".{0,10}"word2"$ , and some other similar combinations but without success.我尝试了以下代码: "word1".{0,10}"word2"$和其他一些类似的组合,但没有成功。

Here are some examples for output:以下是一些输出示例:

x <- c('word1 word2',           # TRUE
       'word1 bla word2',       # TRUE
       'word1 blablabla word2') # FALSE

etc等等


EDIT: I tried all your suggestions but non of them work.编辑:我尝试了您的所有建议,但没有一个有效。 I try to query some data from DATA API.我尝试从 DATA API 查询一些数据。 In query part I have to write what do I want to GET.在查询部分,我必须写出我想要获取的内容。 For example this works: query = list(q = paste0("\\"", "SomeSurname", ". ", "SomeName", ". ", "\\"", "~5", sep = ""))), but if I want to add constraint in the way that there could be maximum 10 arbitrary characters between name and surname it doesn't work: query = list(q = paste0("\\"", prebivaliste[i,"prezime"], ".{0,5}", prebivaliste[i,"ime"],"\\"", "~5", sep = "")))例如这有效: query = list(q = paste0("\\"", "SomeSurname", ". ", "SomeName", ". ", "\\"", "~5", sep = "")) ),但是如果我想以姓名和姓氏之间最多可以有 10 个任意字符的方式添加约束,则它不起作用: query = list(q = paste0("\\"", prebivaliste[i,"prezime" ], ".{0,5}", prebivaliste[i,"ime"],"\\"", "~5", sep = "")))

I think this should match what you're after:我认为这应该符合您的要求:

(word1)(?:.){0,10}(word2)

(word1) - capture the literal text: "word1"
(?:.) - Set up non capturing group for any character
{0,10} - 0-10 times
(word2) - capture the literal text "word2"

grepl('(word1)(?:.){0,10}(word2)', x)
# [1]  TRUE  TRUE FALSE

Assuming you wanted to capture any word instead of word1/word2 you could use \\\\w or \\\\w+ to match假设您想捕获任何单词而不是 word1/word2,您可以使用\\\\w\\\\w+来匹配

Live example: https://regex101.com/r/xJ3yZ2/1实例: https : //regex101.com/r/xJ3yZ2/1

Maybe it is simple with this:也许这很简单:

nchar(gsub('word1|word2','',string))<=10
#[1]  TRUE  TRUE FALSE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM