I have then following pattern Set(?:Value)?
in R as follows:
grepl('Set(?:Value)?', 'Set(Value)', perl=T)
this pattern is macthed by
1- Set
2- Set Value
3- Set(Value)
But I want to match only for two first cases and for for third case. Can anybody help me?
Thank you
You can use
grepl('^Set(?:\\s+Value)?$', x)
grepl('\\bSet(?!\\(Value\\))(?:\\s+Value)?\\b', x, perl=TRUE)
See regex demo #1 and regex demo #2 .
Details :
^Set(?:\\s+Value)?$
- start of string, Set
, an optional sequence of one or more whitespaces ( \s+
) and a Value
and then end of string \bSet(??\(Value\))(:?\s+Value)?\b
:
\b
- word boundary Set
- Set
string (?!\(Value\))
- no (Value)
string allowed at this very location (?:\s+Value)?
- an optional sequence of one or more whitespaces ( \s+
) and a Value
\b
- word boundary See an R demo :
x <- c("Set", "Set Value", "Set(Value)")
grep('^Set(?:\\s+Value)?$', x, value=TRUE)
## => [1] "Set" "Set Value"
grep('\\bSet(?!\\(Value\\))(?:\\s+Value)?\\b', x, perl=TRUE, value=TRUE)
## => [1] "Set" "Set Value"
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.