簡體   English   中英

正則表達式使用模式提取字符串(數字和數字之間的兩個值)

[英]regex use patterns to extract a string (two values between digits and numbers)

我知道有人問過如何在文本中獲取字符串,但直到今天我對regex 我已經合並了一組數據文件,並將文件名放在一列中。 我還需要從文件名中提取X和Y坐標,並將其放在兩列中。

例:

name1_name2_20151028_0_R02X139Y310_1 expected output is
X      Y
139    310

我試過了:

pattern <- "^name1_name2_(\\d+)_0_R(\\d+)X(\\d+)Y(\\d+)_1.*txt$"

當我嘗試運行grep時並沒有成功,因此我無法再前進了。

任何幫助,將不勝感激。 謝謝

讓我們分為兩個步驟:

1. Create a regular expression to extract the information   

gsub("(.*X)([0-9]+)(Y)([0-9]+)(.*)", "\\2, \\4", df$col)

2. Split the information into two columns by the comma

library(splitstackshape) 
df=cSplit(df, "col", ",")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM