ex02ChildrenInverse <- function(sentence) {
assertString(sentence)
matches <- regmatches(
sentence,
regexec('^(.*?) is the (father|mother) of "(.*?)"', sentence))[[1]]
parent <- matches[[2]]
male <- matches[[3]] == "father"
child <- matches[[4]]
child <- gsub('".*"', '', matches[4])
return(list(parent = parent, male = male, child = child))
}
Here is my Code. My Problem is that I want to output the children's Name even though it has double quotations in his name. F.e:
input: 'Gudrun is the mother of "Rosamunde ("Rosi")".'
my output:
$parent
\[1\] "Gudrun"
$male
\[1\] FALSE
$child
\[1\] "Rosamunde ("
but i want
$parent
\[1\] "Gudrun"
$male
\[1\] FALSE
$child
\[1\] "Rosamunde ("Rosi")"
I tried my code and it didn't work out like I wanted to.
I want to change child \<- gsub(.......)
If it alway is the last part of the string, you can match the dot after the last double quote:
For example:
Output
See an R demo and a regex demo