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: Intermediate:
Another regular expression regarding strings

 



Uncle_Jake
Novice

Dec 19, 2013, 12:48 PM

Post #1 of 7 (1696 views)
Another regular expression regarding strings Can't Post

OK...Let's say I've got a file and I want to change the 8th alpha symbol (regardless of case) to a pipe '|' symbol. Here is some sample text:

123abc4defgh12ijk
ABcdefghi987
1abcdefghijk234

In each case, the regular expression will change the 'h' to a pipe (|) symbol.

This, obviously, is incomplete...

$seg3 =~ s/^/\|/g;

And I am endeavoring to find a solution while others may've encountered this many times already.

A big thank you to Kenosis for helping earlier this week.

Uncle Jake


BillKSmith
Veteran

Dec 19, 2013, 5:03 PM

Post #2 of 7 (1687 views)
Re: [Uncle_Jake] Another regular expression regarding strings [In reply to] Can't Post


Code
use strict; 
use warnings;
while (my $line = <DATA>) {
$line =~ s/^((?:[^a-zA-Z]*?[a-zA-Z]){7}[^a-zA-Z]*?)[a-zA-Z]/$1|/;
print $line;
}

__DATA__
123abc4defgh12ijk
ABcdefghi987
1abcdefghijk234:w

Good Luck,
Bill


Kenosis
User

Dec 19, 2013, 8:05 PM

Post #3 of 7 (1681 views)
Re: [Uncle_Jake] Another regular expression regarding strings [In reply to] Can't Post

Here's another option:

Code
use warnings; 
use strict;

while (<DATA>) {
my $i;
s/([a-z])/++$i == 8 ? '|' : $1/ige;
print;
}

__DATA__
123abc4defgh12ijk
ABcdefghi987
1abcdefghijk234


Output:

Code
123abc4defg|12ijk 
ABcdefg|i987
1abcdefg|ijk234


Hope this helps!


(This post was edited by Kenosis on Dec 20, 2013, 10:58 AM)


Uncle_Jake
Novice

Dec 31, 2013, 9:27 AM

Post #4 of 7 (1550 views)
Re: [Kenosis] Another regular expression regarding strings [In reply to] Can't Post

Bill, you and Kenosis are great...I appreciate both of you. Just these two answers have given me impetus to devise more regular expressions that I will use in the future.

Thank you, both!


zapzap
User

Jan 5, 2014, 2:34 AM

Post #5 of 7 (1445 views)
Re: [Kenosis] Another regular expression regarding strings [In reply to] Can't Post

Nice, real nice.


zapzap
User

Jan 5, 2014, 2:39 AM

Post #6 of 7 (1444 views)
Re: [BillKSmith] Another regular expression regarding strings [In reply to] Can't Post

Nice regular expression. I'll have to keep practicing.


Kenosis
User

Jan 5, 2014, 12:44 PM

Post #7 of 7 (1398 views)
Re: [zapzap] Another regular expression regarding strings [In reply to] Can't Post

Thank you, zapzap!

 
 


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

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