Question

I'm trying to search and replace using regex in TextWrangler (https://gist.github.com/ccstone/5385334, http://www.barebones.com/products/textwrangler/textwranglerpower.html)

I have rows like this

56-84   29  STRINGOFLETTERS -2.54

I´d like to replace the dash in "56-84" with a tab, so I get

56    84    29  STRINGOFLETTERS -2.54

But without replacing the dash in "-2.54"

How do I specifically only remove dashes surrounded by numbers on both sides? My regex knowledge is extremelly small, I tried to find [0-9]-[0-9] and replace with [0-9][0-9] but that didnt work.

Était-ce utile?

La solution

Your link says "The PCRE engine (Perl Compatible Regular Expressions) is what BBEdit and TextWrangler use". So hopefully you can use lookaround with your regex.

replace regex:

(?<=\d)-(?=\d)

replace with tab(\t).

Autres conseils

If it's plain text, not sure you need TextWrangler. You can just use the "sed" command of unix:

$ sed 's/\d-\d/\d\d/g' a.txt > b.txt

You actually need to capture the numbers you want. So the regex would be:

^([0-9])-([0-9])

I'm assuming here that the numbers start at the beginning of the line. If not, you can remove the ^.

Based on your link, the flavor of regex is PCRE, so backreferences look like \1, and \2 in the replacement pattern. So your replacement pattern simply becomes:

\1\t\2

Here \1 refers to the first group (so the first number) and \2 refers to the second group (so the second number).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top