Simple REGEX

G

Gabriel

Hello.
I have a file with this kind of data:

field1;field2;field3;field4\n

For example:

Andrew;Dimmu;Shagrath big;This is correct; /* CORRECT LINE */
Jeny; Hello Jen; Spaces;Black; /* INCORRECT LINE */

How can I delete those first spaces (Jeny;Hello Jen;Spaces;Black) ?
$line =~ /;[ ]+//g; ?
 
M

Mirco Wahab

Gabriel said:
I have a file with this kind of data:
field1;field2;field3;field4\n
Andrew;Dimmu;Shagrath big;This is correct; /* CORRECT LINE */
Jeny; Hello Jen; Spaces;Black; /* INCORRECT LINE */

How can I delete those first spaces (Jeny;Hello Jen;Spaces;Black) ?
$line =~ /;[ ]+//g; ?

perl -lpe 's/(?<=;)\s+|^\s+|\s+(?=;)//g' file.txt

This works in these cases:

|
Jeny; Hello Jen; Spaces;Black;
Jenny Space Front; Hello Jen ; Spaces;Black;
Andrew;Dimmu;Shagrath big;This is correct;
|

Regards

M.
 
L

Lambik

Abigail said:
Gabriel ([email protected]) wrote on MMMMCMIV September MCMXCIII in
<URL:)) Hello.
)) I have a file with this kind of data:
))
)) field1;field2;field3;field4\n
))
)) For example:
))
)) Andrew;Dimmu;Shagrath big;This is correct; /* CORRECT LINE */
)) Jeny; Hello Jen; Spaces;Black; /* INCORRECT LINE */
))
)) How can I delete those first spaces (Jeny;Hello Jen;Spaces;Black) ?
)) $line =~ /;[ ]+//g; ?


Almost, but that deletes the semi-colon as well, so you have to put
that one back in:

$line =~ s/; +/;/g;

Just an amendment. This will not work with a space in the first field (read:
beginning of the line).



s/(;) +|^ +/$1/g;
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top