简体   繁体   中英

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.

I have tried the following code: "word1".{0,10}"word2"$ , and some other similar combinations but without success.

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. 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 = "")))

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

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

Maybe it is simple with this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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