Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expressions

Status
Not open for further replies.

safra

Technical User
Jan 24, 2001
319
0
0
NL
Hi,

I can't get this to work. I simply like to return all characters in front of '>' in a string to a new string.

I tried it with $& which worked in a simple testing script but not in the actual script where I have to use it.

so I tried things like for example:
$original = "before>after";
$myString = $original;
$myString =~ /(^.*>)/;

it doesn't return anything!

Can anyone show me how to do this?

Thanks,
Ron




 
What you tried
Code:
$myString =~ /(^.*>)/;
only matches what you are looking for, it doesn't remove it. For that you need s///
Code:
$mystring =~ s/^.*?>//;
I added the '?' because it matches a little faster. It will find the first '>' instead of greedily matching everything up to the last '>'. So for the string 'before>middle>after' with the '?' would remove 'before>', but without the '?' would remove 'before>middle>'.

jaa
 
hi,

You can use the following perl special variables,
$` ->stores whatever precedes the currently matched pattern
$& ->The pattern that you gave.
$' -> The characters that follow the given pattern.

So in your case,

$` will have "before"
$& will hold ">" and
$' will contain "after"


Note:Using the above variables is not very efficient.

But it will get you what you wanted.


regards,
VGG


 
Oops, slight modification...
Code:
$myString =~ s/>.*?$//;
that will remove the '>' and everything after it, leaving $myString == 'before'. (I misread what you wanted to keep).

jaa
 
thanks justice41, that worked!

VGG, I struggled with your solution. I can't get the matching to work. I assume I first have to look for the first '>' in the string. Nothing works,$' and $& are always empty and $' contains the complete string.

thanks,
Ron
 
Yes thats right.
Specify the pattern as />/ and check.
I am presently not on a PERL assignment so I am
helpless in testing the solution.

VGG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top