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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I extract substrings into variables

Status
Not open for further replies.

akelabanda

Programmer
Joined
Dec 19, 2001
Messages
61
Location
IN
Hi

I want some help in pattern matching. Given a String, I want to
extract portions and store in variables.

For example, the following is the string

# 220 xxxx.com -- Server ESMTP (iPlanet Messaging Server 5.0 (built Oct 12 2000))

I want to extract "iPlanet Messaging Server" in one variable
and "5.0" in another.

Right now I'm splitting on "(" and then substr'ing for the above
separately.

Is there a better way to do this ? I'm not a master at "pattern
matching - regular expr"

Thanks for your help in advance.

Regards
Rajeev
 
You can try this:

$_ = '220 xxxx.com -- Server ESMTP (iPlanet Messaging Server 5.0 (built Oct 12 2000))';

($server,$version) = /.*\(([\w ]*) ([\d.]+)\s*\(/;

print "Server: $x\nVersion: $y\n\n";


#=== end of code.

This method assumes that your input string will always be in the format of:

blah blah ([server name here] [server version here] ( blah

The parens placement is important. The blah blah can be anything. The server name can contain letters, digits and underscores. The Version number may only contain digits and periods.

Hope this helps.

--jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top