Update: As per comments regarding the ambiguity of my question, I've increased the detail in the question.
(Terminology: by words I am refering to any succession of alphanumerical characters.)
I'm looking for a regex to match the following, verbatim:
I would like to match the following, however not verbatim, rather, removing the apostrophes:
'foo'
would be matched to foo
.foo''bar
would be matched to foo
and bar
.''foo
would be matched to foo
and ''foo''
to foo
.Examples These would be matched verbatim:
'bout
it's
persons'
But these would be ignored:
'
''
And, for 'open'
, open
would be matched.
How do you escape an apostrophe in regex? The apostrophe is a special character and needs to be escaped from the standard text by prefixing with '\', try pattern="^([a-zA-Z\'-]+)$" HTH.
Escape sequences For example, apostrophes. Apostrophes can be used in R to define strings (as well as quotation marks). For example name <- 'Cote d'Ivore'' will return an error. When we want to use an apostrophe as an apostrophe and not a string delimiter, we need to use the “escape” character \' .
The \b metacharacter matches at the beginning or end of a word.
The regular expression \s is a predefined character class. It indicates a single whitespace character. Let's review the set of whitespace characters: [ \t\n\x0B\f\r]
(?=.*\w)^(\w|')+$
'bout # pass
it's # pass
persons' # pass
' # fail
'' # fail
NODE EXPLANATION
(?= look ahead to see if there is:
.* any character except \n (0 or more times
(matching the most amount possible))
\w word characters (a-z, A-Z, 0-9, _)
) end of look-ahead
^ the beginning of the string
( group and capture to \1 (1 or more times
(matching the most amount possible)):
\w word characters (a-z, A-Z, 0-9, _)
| OR
' '\''
)+ end of \1 (NOTE: because you're using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)
$ before an optional \n, and the end of the
string
/('\w+)|(\w+'\w+)|(\w+')|(\w+)/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With