Issue
I have seen in manuals how to use grep to match either a pattern or an exact string. However, I cannot figure out how to do both at the same time. I have a latex file where I want to find the following pattern:
\caption[SOME WORDS]
and replace it with:
\caption[\textit{SOME WORDS}]
I have tried with:
texfile <- sub('\\caption[','\\caption[\\textit ', texfile, fixed=TRUE)
but I do not know how to tell grep that there should be some text after the square bracket, and then a closed square bracket.
Solution
You can use
texfile <- "\\caption[SOME WORDS]" ## -> \caption[\textit{SOME WORDS}]
texfile <-gsub('(\\\\caption\\[)([^][]*)]','\\1\\\\textit{\\2}]', texfile)
cat(texfile)
## -> \caption[\textit{SOME WORDS}]
See the R demo online.
Details:
(\\caption\[)
- Group 1 (\1
in the replacement pattern): a\caption[
string([^][]*)
- Group 2 (\2
in the replacement pattern): any zero or more chars other than[
and]
]
- a]
char.
Another solution based on a PCRE regex:
gsub('\\Q\\caption[\\E\\K([^][]*)]','\\\\textit{\\1}]', texfile, perl=TRUE)
See this R demo online. Details:
\Q
- start "quoting", i.e. treating the patterns to the right as literal text\caption[
- a literal fixed string\E
- stop quoting the pattern\K
- omit text matched so far([^][]*)
- Group 1 (\1
): any zero or more non-bracket chars]
- a]
char.
Answered By - Wiktor Stribiżew Answer Checked By - Senaida (WPSolving Volunteer)