搜索并替换字符串

时间:2017-12-01 06:05:12

标签: r regex string gsub

需要命令帮助从开始和第一个数字之前替换字符串中的文本。我有一个数据框,在单个字段中包含所有这些地名和地址,但只需要从字符串中提取地址。

使用此gsub命令替换字符串

gsub("^.*?\\d","","Gilroy Plant Place 777 Morello Ave")

[1] "77 Morello Ave"

必需的输出:

777 Morello Ave

3 个答案:

答案 0 :(得分:1)

我会使用<div style="width: 500px; max-width: 100%; background: white; height: 500px; position: relative; overflow:hidden; border: 4px solid white;"> <div style="position: absolute; width: 250px; height: 250px; -webkit-clip-path:polygon(0 0, 0 100%, 100% 0%, 100% 0); clip-path:polygon(0 0, 0 100%, 100% 0%, 100% 0); -moz-clip-path:polygon(0 0, 0 100%, 100% 0%, 100% 0); background: darkred;"></div> <div style="bottom: 0; right: 0px; position: absolute; width: 250px; height: 250px; -webkit-clip-path:polygon(0 0, 0 100%, 100% 0%, 100% 0); clip-path:polygon(0 0, 0 100%, 100% 0%, 100% 0); -moz-clip-path:polygon(0 0, 0 100%, 100% 0%, 100% 0); background: #222; transform: rotate(180deg); overflow: hidden;"> <div style="width: 250px; height: 315px; -ms-transform: rotate(-45deg); transform: rotate(180deg); background: red; background-position: center; margin: 0px 20px 0px 0px;">...</div> </div> <div style=" position: absolute; background: #230000; height: 150px; width: 150px; border-radius: 100%; z-index: 99999999999; left: 34%; bottom: 34%; border: 4px solid white;">...</div> <div style="bottom: -58px; left: -58px; position: absolute; background: #222; height: 355px; width: 355px; transform: rotate(45deg); border: white solid 4px; overflow:hidden;"> <div style="width: 400px; height: 375px; -ms-transform: rotate(-45deg); transform: rotate(-45deg); background: #550000; background-position: center; margin: -106px 0px 0px -15px;">...</div> </div> <div style="top: -58px; right: -58px; position: absolute; background: violet; height: 355px; width: 355px; transform: rotate(45deg); border: white solid 4px; overflow: hidden"> <div style="background: black; width: 400px; height: 375px; -ms-transform: rotate(-45deg); transform: rotate(-45deg); background: #070000; background-position: center; margin: 75px 0px 0px -15px;">...</div> </div> </div>这个东西匹配任何字符但不匹配数字。 \D表示从一开始^\D*匹配零个或多个^个非数字字符*

\D

答案 1 :(得分:0)

希望这有帮助!

gsub(".*?(\\d.*)","\\1","Gilroy Plant Place 777 Morello Ave")

答案 2 :(得分:0)

使用积极的前瞻:

^.*?(?=\d)

前瞻性的前瞻是它看向字符串前面并查看子模式是否匹配。如果是,则匹配子模式之前的内容。在上面的正则表达式中,^.*?只会匹配\d后匹配的字符串,但\d部分不会匹配。