Regex to replace 'string \t' with 'string |'

1.7k views Asked by At

I've this kind of file (a csv):

(\t = tab)

ID \t   Name \t Root Bridge \t  Count
0   \t default  \t gateway1 \t 2

In Gedit (using the 'regex' mark check), I need to replace tabs with | to make it a table compatible with the Docuwiki format.

I thought that because I've a recurrent pattern of %string% \t I could easily replace that with a regex that said %string% |.

But it looks like I'm missing something.

I tried with s/[.*\t]/[.*|]/g but it doesn't work.

I probably didn't understand something basic about how regex works.

Thanks in advance

PS: this is my first question on StackExchange and tried to follow the recommendations on "how to write a question". I hope I got it right.

3

There are 3 answers

0
rheese On BEST ANSWER

I don't understand why you need regex, but if you want exactly the following:

replace 'string \t' with 'string |'

you can use this

s/([^\t]+)\t/\1 |/g 

in gedit or notepad++ refers to the matched group are notified with \groupnumber, in othe rlanguage with $groupnumber.

0
brtsos On

You can try (\t|\|). Should working.

0
Hubert Grzeskowiak On

Not sure why you need regex for this. Just copy-paste a tab character and do a simple "search & replace", which works in any editor.