R:提取括号内的字符串/数字以及方括号内的字符串序列

时间:2016-04-26 02:34:08

标签: regex r text-mining tidyr data-cleaning

我在将数据分成两列或三列时遇到了一些问题。

以下是我想要处理以下列的字符串

station "Park Rd & Holmead Pl NW (31602)"

我该怎么做才能将它分成两列:

station address   "Park Rd & Holmead Pl NW "             
station number    31602

以类似的方式, 我怎么能分开以下字符串?

station  "Park Rd & Holmead Pl NW (formerly 34th & Water St NW)"

成:

station         "Park Rd & Holmead Pl NW"                          
former station  "34th & Water St NW"

任何善于正常表达的人,请帮帮我!

谢谢!

1 个答案:

答案 0 :(得分:1)

# question 1
x <- "Park Rd & Holmead Pl NW (31602)"
strsplit(x, "\\(|)$")[[1]]

# question 2
x <- "Park Rd & Holmead Pl NW (formerly 34th & Water St NW)"
strsplit(x, "\\(formerly |)$")[[1]]