|
-
Oct 22nd, 2001, 05:43 AM
#1
Thread Starter
Addicted Member
perl regular expressions
I am using the following string:
#abc#bcd#efg#hij#klm#nop
I need to write a regular expression which will find the 3rd hash and extract the entire line, including the hash to give me the following output:
#efg#hij#klm#nop
-
Oct 22nd, 2001, 09:57 AM
#2
Frenzied Member
Wow... well, my code is going to be rusty, to check the monastary.
Code:
$string =~ s/^\#.*\#.*\#/\#/;
You may be able to use # instead of \#. This is just one solution. There is also the backreferences. In any case, I strongly suggest you get the Camel book if no other. It is very useful.
Curses... it is up to 3rd Ed. Perhaps I should... uhm... loose mine so I can buy a new one. Someone still has my Cookbook. Guess I need to replace that, too.
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
-
Oct 23rd, 2001, 04:38 AM
#3
Thread Starter
Addicted Member
Thanks Travis, I will certainly look into getting that book !
Also,
I'm still having problems, sorry but the string is supposed to look as follows:
my $string = "abc#ccd#egg#hij#klm#nop#rst#luv#wxyx#";
The string is not a fixed length, it may be longer than this but the pattern will always be the same - to ignore all characters before the 5th Hash, in this example I want to extract the following:
#nop#rst#luv#wxyx#
Any help will be greatly appreciated.
Thanks.
-
Oct 23rd, 2001, 08:45 AM
#4
Frenzied Member
Well, to remove the everything before the fifth pound...
Code:
$string =~ s/^(\#.*){4}\#/\#/;
# or
$string =~ s/^(#.*){4}#/#/;
Like I said, I don't think you need to escape the pounds, but it shouldn't break anything if you do.
Now this will modify the string, so you will loose everything at the beginning of the string. There are way around that. You could copy the string and work the with copy.
Another idea is....
Code:
my $string; #the pound enriched string
my @subStrings; #each element is a peice of $string without the #'s
my $result; #the resulting string
my $targetPound; #this is the index of the #s in the string that you want to start reading. 0 is the first
@subStrings = split /#/, $string;
$result = join /#/, $subStrings[$targetPound .. -1]
Travis, Kung Foo Journeyman
As always, RTFM.
WWW Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.3 Guide and Reference
Perl: Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
YBMS, but Mozilla doesn't.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|