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:
"123 456" to "123456"

 



drothman66
Novice

Mar 4, 2010, 11:24 AM

Post #1 of 10 (9046 views)
"123 456" to "123456" Can't Post

i know..it's probably simple, but i know how to find 3nums, space, 3nums within a larger string...

i just don't know how to elim (with regex) the space so that "123 456" becomes "123456".

thanks.


(This post was edited by drothman66 on Mar 4, 2010, 11:32 AM)


toolic
User

Mar 4, 2010, 5:36 PM

Post #2 of 10 (9025 views)
Re: [drothman66] "123 456" to "123456" [In reply to] Can't Post

You could use the substitution operator:

Code
my $str = '123 456'; 
$str =~ s/\s//;



drothman66
Novice

Mar 4, 2010, 5:38 PM

Post #3 of 10 (9024 views)
Re: [toolic] "123 456" to "123456" [In reply to] Can't Post

that only works when the string is the "123 456".

i need an idiom for when the construct is part of a longer string, ala:

"hello 123 456 goodbye" becomes
"hello 123456 goodbye"


toolic
User

Mar 4, 2010, 5:41 PM

Post #4 of 10 (9023 views)
Re: [drothman66] "123 456" to "123456" [In reply to] Can't Post

http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators


rovf
Veteran

Mar 5, 2010, 4:55 AM

Post #5 of 10 (8983 views)
Re: [drothman66] "123 456" to "123456" [In reply to] Can't Post

You can use parenthesis to capture your 3-digit string groups, and then use backreferences to glue them together again. For instance, to remove every sequence of spaces which occurs between digits, I would use

$mystring =~ s/(\d)\s+(\d)/$1$2/g


shawnhcorey
Enthusiast


Mar 5, 2010, 5:59 AM

Post #6 of 10 (8976 views)
Re: [drothman66] "123 456" to "123456" [In reply to] Can't Post

Try:

Code
#!/usr/bin/perl 

use strict;
use warnings;

my $text = "hello 123 456 goodbye";
print "$text\n";

$text =~ s{ (?<=[[:digit:]]{3}) \s+ (?=[[:digit:]]{3}) }{}msx;
print "$text\n";


__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".


drothman66
Novice

Mar 5, 2010, 9:54 AM

Post #7 of 10 (8963 views)
Re: [shawnhcorey] "123 456" to "123456" [In reply to] Can't Post

not only does that work, it works for all the cases i can think of (when i add the g switch). this is great..thanks....

my purpose, by the way, is to clean up poorly entered phone numbers that i'm am sucking in directly from a series of Excel spreadsheets.

i simply want to grab the numbers and eliminate "()\c-" (which i can simply do sequentially with your idiom). When they're imported back into excel, excel can format them correctly.

as i'm grabbing records as strings and dumping them to a .csv, i needed something which would work on more than just one field.

thanks again


drothman66
Novice

Mar 5, 2010, 12:50 PM

Post #8 of 10 (8954 views)
Re: [drothman66] "123 456" to "123456" [In reply to] Can't Post

Hi. I played around with the syntax, but can't quite get this similar (but easier problem):

"hello (123) goodbye" becomes:

"hello 123 goodbye"

i'd then be able to deal with most of the cases (in multiple passes, but that's ok. thanks again.


shawnhcorey
Enthusiast


Mar 5, 2010, 4:07 PM

Post #9 of 10 (8945 views)
Re: [drothman66] "123 456" to "123456" [In reply to] Can't Post

Try:

Code
#!/usr/bin/perl 

use strict;
use warnings;

my $text = "hello (123) goodbye";
print "$text\n";

$text =~ s{ \( ([[:digit:]]{3}) \) }{$1}msx;
print "$text\n";


__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".


drothman66
Novice

Mar 6, 2010, 5:10 AM

Post #10 of 10 (8910 views)
Re: [shawnhcorey] "123 456" to "123456" [In reply to] Can't Post

Got it. This one was much easier than last one. I've got some work to do to get from here to there...thanks again.

 
 


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

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