Regular Express to remove all text between to ,'s

S

Stitch Jones

I'm trying to get part of my perl script using a regex to get rid of
an entire field between two ,'s I'm doing this to get a .csv formated
alot nicer. here is an example of my issue
user ,jibberjibber 1234456hithere(hkl)- special char [0]jibber, 4567
I need the keep the user part and the end 4567 part but all the stuff
between the two ,'s has gotta go. Someone Please help.
Thank you.
 
S

Simon Proctor

Stitch said:
I'm trying to get part of my perl script using a regex to get rid of
an entire field between two ,'s I'm doing this to get a .csv formated
alot nicer. here is an example of my issue
user ,jibberjibber 1234456hithere(hkl)- special char [0]jibber, 4567
I need the keep the user part and the end 4567 part but all the stuff
between the two ,'s has gotta go. Someone Please help.
Thank you.

s!^([^,]*),[^,]*?,(.*)$!$1$2!;

This assumes that the line given is formatted as you defined and you only
want to remove the first lot of comma defined text.
 
S

Shawn Zabel

Stitch Jones said:
I'm trying to get part of my perl script using a regex to get rid of
an entire field between two ,'s I'm doing this to get a .csv formated
alot nicer. here is an example of my issue
user ,jibberjibber 1234456hithere(hkl)- special char [0]jibber, 4567
I need the keep the user part and the end 4567 part but all the stuff
between the two ,'s has gotta go. Someone Please help.
Thank you.


The following regex will substitute everything between the first and last
comma (including the commas on each end) with a comma. The result based on
your example will be "user , 4567" without the quotes.

s/,.*,/,/


- Shawn
 
J

Joe Smith

Stitch said:
I'm trying to get part of my perl script using a regex to get rid of
an entire field between two ,'s

What have you tried? Post a snippet of your code to the proper
newsgroup (comp.lang.perl.misc) instead of here (comp.lang.perl).
[Do you know the difference between s/,.*,/,/ and s/,.*?,/,/ ?]
-Joe
 
S

Stitch Jones

Stitch Jones said:
I'm trying to get part of my perl script using a regex to get rid of
an entire field between two ,'s I'm doing this to get a .csv formated
alot nicer. here is an example of my issue
user ,jibberjibber 1234456hithere(hkl)- special char [0]jibber, 4567
I need the keep the user part and the end 4567 part but all the stuff
between the two ,'s has gotta go. Someone Please help.
Thank you.


The following regex will substitute everything between the first and last
comma (including the commas on each end) with a comma. The result based on
your example will be "user , 4567" without the quotes.

s/,.*,/,/


- Shawn

Shawn, Thank you. That was what I needed. I was overthinking it. I
thought I needed to come up with a fancy string to parse through it.
I'll have to start brushing up on my Reg-Ex 101 stuff.
Thank You.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top