简体   繁体   English

正则表达式删除R中除数字,字母和空格外的所有内容

[英]Regex to Remove Everything but Numbers, Letters and Spaces in R

How can I remove these pesky backslashes in R? 如何在R中删除这些讨厌的反斜杠? I've scoured the web and stackoverflow to try to find a way to get rid of backslashes...no luck. 我搜寻了网络和stackoverflow以尝试找到一种方法来摆脱反斜杠...没有运气。

I've tried a lot of different ways, but I think the only one that I can get working will be to remove every character that is not a number, letter or space using regular expressions and gsub(). 我尝试了很多不同的方法,但是我认为唯一可以使用的方法是使用正则表达式和gsub()删除每个不是数字,字母或空格的字符。 Here is my string: 这是我的字符串:

"_kMDItemOwnerUserID = 99kMDItemAlternateNames = ( \"(500) Days of Summer     (2009).m4v\")kMDItemAudioBitRate = 163kMDItemAudioChannelCount =     2kMDItemAudioEncodingApplication = \"HandBrake 0.9.4 2009112300\"kMDItemCodecs =     ( \"H.264\", AAC, \"QuickTime Text\")"

As you can see it is very messy, with backslashes and quotation marks all over the place. 如您所见,它非常混乱,到处都有反斜杠和引号。 Ultimately, what I want to do is extract the movie name: '(500) Days of Summer (2009)'. 最终,我要提取的电影名称是:“((500)Days of Summer(2009))”。

What is a regular expression that will match everything but numbers, letters and spaces? 什么是匹配数字,字母和空格的所有内容的正则表达式?

Thank you very much in advance for your help. 预先非常感谢您的帮助。

gsub("[^[:alnum:] ]", "", x)

Try replacing the character class [^[:alnum:] ] , which will match any character which is not a letter, number, or space: 尝试替换字符类[^[:alnum:] ] ,它将匹配不是字母,数字或空格的任何字符:

Full code: 完整代码:

x <- "_kMDItemOwnerUserID = 99kMDItemAlternateNames = ( \"(500) Days of Summer     (2009).m4v\")kMDItemAudioBitRate = 163kMDItemAudioChannelCount =     2kMDItemAudioEncodingApplication = \"HandBrake 0.9.4 2009112300\"kMDItemCodecs =     ( \"H.264\", AAC, \"QuickTime Text\")"

gsub("[^[:alnum:] ]", "", x)
[1] "kMDItemOwnerUserID  99kMDItemAlternateNames   500 Days of Summer     2009m4vkMDItemAudioBitRate  163kMDItemAudioChannelCount      2kMDItemAudioEncodingApplication  HandBrake 094 2009112300kMDItemCodecs       H264 AAC QuickTime Text"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM