我遇到的问题很奇怪。假设我有:
a = "www.XXXXXXX.com"
b = "www.XXXXXXX.com/laskdfj/=*&9809f/12-613"
c = "www.XXXX.comllkjldfjlsadjfjldsf"
d = "http://www.XXXX.CoMmasldfjl"
e = "www.XXX.us/sdf"
f = "www.XXX.us0948klsdf"
如果在“.com”或“.us”之后没有斜线,则将其删除。结果就像是:
a = "www.XXXXXXX.com"
b = "www.XXXXXXX.com/laskdfj/=*&9809f/12-613"
c = "www.XXXX.com"
d = "http://www.XXXX.CoM"
e = "www.XXX.us/sdf"
f = "www.XXX.us"
正则表达对我来说是新的,我读了几篇关于正则表达式的博客,似乎没有人谈论如何使用if语句来处理我的情况......任何提示?
答案 0 :(得分:3)
您可以使用sub
执行此任务:
sub('(.*\\.(?i:com|us))[^/]+', '\\1', x)
如果您想要更通用的方法,可以使用:
sub('(.*\\.[[:alpha:]]{2,3})[^/]*', '\\1', x)