2017-09-25 52 views
1

我想兩個製表師加入到後一+姓(\nJohn Smith\t)發生的TSV文件:如何使用替換與捕獲組?

dl=readstring(fileName) 
dl=replace(dl,r"(\n[A-Za-z\s]+\t)","\1\t\t") 

我得到一次+姓換成x01。該文件說一些關於替換字符串,但我找不到執行

更新:此替代了一組

dl=replace(dl,r"(\n[A-Za-z\s]+\t)",s"\1") 

但這:

dl=replace(dl,r"(\n[A-Za-z\s]+\t)",s"\1\t\t") 

導致錯誤Bad Replace。沒有\的符號顯得很好。

回答

2

這對我來說似乎是個bug。但是,你可以使用的解決方法:

julia> dl = "\nJohn Smith\t"; 
julia> s = Base.SubstitutionString; 
julia> dl=replace(dl, r"(\n[A-Za-z\s]+\t)", s("\\1\t")) 
"\nJohn Smith\t\t" 

編輯:

我覺得這是更好的:

julia> dl = "\nJohn Smith\t"; 
julia> dl=replace(dl, r"(\n[A-Za-z\s]+\t)", @s_str("\\1\t")) 
"\nJohn Smith\t\t" 

順便說一句,如果你想捕捉組後,追加數量,那麼你可以做其他的竅門(命名組):

julia> replace("aAa", r"(?<one>A+)", s"\g<one>1") 
"aA1a" 
相關問題