简体   繁体   English

删除字符串中的所有内容,但删除R中的空格,破折号和字母?

[英]Remove everything from string but spaces, dashes and letters in R?

I am trying to remove everything in a string except spaces dashes(-) and letters. 我正在尝试删除字符串中除空格破折号(-)和字母之外的所有内容。 For example 例如

string1 <- "test-%432string *#$ one!~+"

how do I return "test-string one" 如何返回“测试字符串一个”

I tried : gsub("[^a-zA-Z-\\s]", "", string1) to no avail -- it removes the space, which should be left. 我尝试了: gsub("[^a-zA-Z-\\s]", "", string1)无济于事-它删除了应该留的空格。

Thanks for any help. 谢谢你的帮助。

Try this 尝试这个

/[^\w\-\s]|\d/

That worked for me. 这对我有用。 You can try it out on rubular.com. 您可以在rubular.com上尝试一下。 Enjoy. 请享用。

Or in R form: 或以R形式:

gsub("[^\\w\\-\\s]|\\d","",string1,perl = TRUE)
[1] "test-string  one"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP去除字符串(除了字母和空格),然后将空格转换为破折号 - PHP strip string of everything but letters and spaces, then convert spaces to dashes 正则表达式删除R中除数字,字母和空格外的所有内容 - Regex to Remove Everything but Numbers, Letters and Spaces in R 除了PHP字符串中的字母以外的所 - Remove everything except letters from PHP string 在Python3.3中从字符串中删除除字母和空格以外的所有内容 - Removing everything except letters and spaces from string in Python3.3 如何在javascript中删除字符串中的所有非字母并将空格转换为破折号? - How can I strip all non-letters from string and convert spaces to dashes in javascript? 替换字符串中的所有字符(字母和空格除外) - Replace everything in string except letters and spaces 如何将unicode字符串检查为仅字母,空格和破折号 - How to check unicode string to be letters, spaces and dashes only 从电话号码中删除括号、破折号和空格 - Remove parentheses, dashes, and spaces from phone number Python:从字符串中删除除字母和空格之外的所有内容 - Python: Remove everything except letters and whitespaces from string “空格”、“破折号 (-)”、“撇号 (&#39;)”和“字母”的正则表达式 - Regex for "spaces", "dashes (-)", "apostrophes (')", and "letters"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM