Home Download Buy Blog Forum Support

regex to insert, not replace

regex to insert, not replace

Postby dnk on Wed Jul 17, 2013 10:39 pm

Hi there, I am using sublime text since it supports regex in the search field. I am essentially running a match for:

Code: Select all
\s6[0-9][0-9][0-9]\s


And then what I want to do is insert a new line (\n) in front of that (not replace it). My Sample data looks like:

Code: Select all
6309 ,9044XXXXXXXXXX ,"Jul 8, 2013 5:49:36 AM",42 ,Others,Some Name 6207 ,901XXXXXXXXXX ,"Jul 8, 2013 6:13:50 AM",4 ,Others,Another Name 6211 ,9044XXXXXXXXXX ,"Jul 8, 2013 6:22:00 AM",24 ,Others,Onemore Name


And I want to get it to:

Code: Select all
6309 ,9044XXXXXXXXXX ,"Jul 8, 2013 5:49:36 AM",42 ,Others,Some Name
6207 ,901XXXXXXXXXX ,"Jul 8, 2013 6:13:50 AM",4 ,Others,Another Name
6211 ,9044XXXXXXXXXX ,"Jul 8, 2013 6:22:00 AM",24 ,Others,Onemore Name


The issue I have , is that since each line may have a different 6XXX number, I cannot just replace 6XXX with \n6XXX - unless there is some way to pass the actual match over and reinsert it....

Any ideas?
Thanks!
dnk
 
Posts: 4
Joined: Wed Jul 17, 2013 10:36 pm

Re: regex to insert, not replace

Postby qgates on Wed Jul 17, 2013 11:57 pm

Search:
Code: Select all
(6[0-9]{3})

replace:
Code: Select all
\n\1

:)
qgates
 
Posts: 230
Joined: Mon Nov 05, 2012 4:33 pm

Re: regex to insert, not replace

Postby dnk on Thu Jul 18, 2013 5:06 pm

qgates wrote:Search:
Code: Select all
(6[0-9]{3})

replace:
Code: Select all
\n\1

:)


You sir, are a gentleman and a scholar. That was driving me nuts!
dnk
 
Posts: 4
Joined: Wed Jul 17, 2013 10:36 pm

Re: regex to insert, not replace

Postby dnk on Thu Jul 18, 2013 5:23 pm

qgates wrote:Search:
Code: Select all
(6[0-9]{3})

replace:
Code: Select all
\n\1

:)



OOPS.... I forgot to mention, that in the sample data, the "XXXXXX"'s I had listed above are also numbers (I was just removing the real phone numbers). So for example, the sample data may actually look like:

Code: Select all
6309 ,90446150000000 ,"Jul 8, 2013 5:49:36 AM",42 ,Others,Some Name 6207 ,90446250000000 ,"Jul 8, 2013 6:13:50 AM",4 ,Others,SomeOther Name 6211 ,90446350000000 ,"Jul 8, 2013 6:22:00 AM",24 ,Others,Another Name 6353 ,90446450000000 ,"Jul 8, 2013 6:22:05 AM",50 ,Others,Name Name 6236 ,90446550000000 ,"Jul 8, 2013 6:37:16 AM",21 ,Others,Last Name


So based on what you posted, the pattern may also match on the full phone numbers.

IE.


6309 ,90446150000000 ,"Jul 8, 2013 5:49:36 AM",42 ,Others,Some Name


Then I end up with another split in the ,middle as well.

So, If i take your example and try...

Code: Select all
\s(6[0-9]{3})\s


My assumption is that it will work.

Just posting for a complete answer. I'll test and reply back once I get to my sample data.
dnk
 
Posts: 4
Joined: Wed Jul 17, 2013 10:36 pm

Re: regex to insert, not replace

Postby adzenith on Thu Jul 18, 2013 6:23 pm

You can also use \d to match a digit and \D to match a non-digit.
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm


Return to General Discussion

Who is online

Users browsing this forum: applejosh, Google [Bot] and 21 guests