CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
regex - skip sub-section ?

 



bugman
New User

Jul 8, 2009, 6:58 AM

Post #1 of 5 (3919 views)
regex - skip sub-section ? Can't Post

Hello everyone,
I have a general question about regex in Perl:

Is it possible to skip some sub-section of a line while making a search & replacement?

An example, I have the line:

bla bla, bla "bla, bla"

Say I would like to replace the commas with a dot, but not when they appear in the section between double quotes.
Would it be possible to skip the second comma in this line by somehow matching also the section between quotes?

Maybe this is something really easy but I just couldn't find it.
Thanks in advance for all replies.


shawnhcorey
Enthusiast


Jul 8, 2009, 11:14 AM

Post #2 of 5 (3915 views)
Re: [bugman] regex - skip sub-section ? [In reply to] Can't Post


In Reply To
bla bla, bla "bla, bla"

Say I would like to replace the commas with a dot, but not when they appear in the section between double quotes.
Would it be possible to skip the second comma in this line by somehow matching also the section between quotes?

Maybe this is something really easy but I just couldn't find it.
Thanks in advance for all replies.


If you're working with a CSV file, use the module CSV_XS.

If it's ALWAYS the second comma, then you can use:

Code
s{ \A ( [^,]* \, [^,]* ) \, }{$1.}msx;

otherwise use the module.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


ichi
User

Jul 9, 2009, 1:49 AM

Post #3 of 5 (3909 views)
Re: [bugman] regex - skip sub-section ? [In reply to] Can't Post


Code
while (<>) { 
chomp;
@f = split(/\"/, $_);
for ($i = 0; $i <= $#f; $i += 2) {
$f[$i] =~ s/,/./g;
}
print join("",@f);
}


output

Code
# more file 
bla bla, bla "bla1, bla1" , sdf sdfs,fdf, dsff, "xxxx,xxx,yyy,zzz" ,dff
# ./test.pl file
bla bla. bla bla1, bla1 . sdf sdfs.fdf. dsff. xxxx,xxx,yyy,zzz .dff

is this what you want? i know the output doesn't have the " anymore, but its easy to put it back. so i leave it to you.


bugman
New User

Jul 9, 2009, 3:00 AM

Post #4 of 5 (3906 views)
Re: [ichi] regex - skip sub-section ? [In reply to] Can't Post

I will reply in order:

@shawnhcorey
1. I am not using a CSV file unfortunately and the comma doesn't have to be always the second one, it could be inside quotes anywhere in the segment.
2. I checked about the __END__ module but as far as I could see, it just ignores anything after it. So in case I would have other commas that need modification after the quotes, these ones would also be skipped, which is not the desired case for me. But is there a way to wrap a section in the line, maybe smt like <skip> ... </skip> ? so that it only ignores that part?

@ichi
This template could be really nice for me, but I will need to work on it I guess a bit more to cover all different possibilities, of where this section might occur.
In case the quote is first character, would this code assign the "beginning of line" to the 0th element?
Anyway I will just test it asap.

Thanks again both answers.


KevinR
Veteran


Jul 9, 2009, 4:39 PM

Post #5 of 5 (3895 views)
Re: [bugman] regex - skip sub-section ? [In reply to] Can't Post

I am sure you can use the Text::ParseWords module that comes with perl to do what you want. Read the documentation and give it a try.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives