哪个正则表达式从文本中的引号中删除标点符号 [英] Which regex removes punctuation from quotation marks in text
问题描述
我有一个数据库,在整个文本中有一些引号是引号.我想删除所有的点."文中用引号括起来.
I have a database and throughout the text there are some quotes that are in quotation marks. I would like to remove all the dots "." that are enclosed in quotation marks in the text.
我有用引号标记文本的代码,但如果有多个引号或多个点,则仅删除第一个.
I have code that punctuates text in quotation marks but if there is more than one quote or more than one point, only the first one is removed.
# Simple phrase:
string <- '"é preciso olhar para o futuro. vou atuar" no front '
# Code that works for a simple 1-point sentence:
str_replace_all(string, '(\".*)\\.(.*\")','\\1\\2')
# Sentence with more than one point and more than one quote:
string <- '"é preciso olhar para o futuro. vou atuar" no front em que posso
fazer alguma coisa "para .frente", disse jose.'
# it doesn't work as i would like
str_replace_all(string, '(\".*)\\.(.*\")','\\1\\2')
我想把引号中的所有点都去掉,但是你可以从例子中看到我开发的正则表达式不适用于更一般的情况.
I would like all the points in quotation marks to be removed, but you can see from the example that the regex I developed is not for more general cases.
推荐答案
你可以简单地使用 str_replace_all
和一个 "[^"]*"
模式并使用一个回调函数作为替换参数,使用 gsub
调用删除所有点:
You may simply use str_replace_all
with a mere "[^"]*"
pattern and use a callback function as the replacement argument to remove all dots with a gsub
call:
str_replace_all(string, '"[^"]*"', function(x) gsub(".", "", x, fixed=TRUE))
所以,
"[^"]*"
匹配string
中以"
开头的所有子字符串,然后包含除以外的 0+ 个字符"
然后是"
- 一旦找到匹配项,它就会作为
x
传递给回调,其中gsub(".", "", x, fixed=TRUE)
替换所有.
(fixed=TRUE
使它成为一个文字点,而不是一个正则表达式模式)带有一个空字符串.
"[^"]*"
matches all substrings instring
starting with"
, then having 0+ chars other than"
and then a"
- Once the match is found, it is passed to the callback as
x
wheregsub(".", "", x, fixed=TRUE)
replaces all.
(fixed=TRUE
makes it a literal dot, not a regex pattern) with an empty string.
这篇关于哪个正则表达式从文本中的引号中删除标点符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!