Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
return part of a string that matches a pattern?

Posted by khuennekens (khuennekens), 13 January 2003
I am reading in a file as a long string.  There are certain key words in this file that separate different pieces of information.  As an example, let's say you have a string like this: "one here is some information two here's some other info three even more info four it's all different five", and you want to know what is the information between the words "two" and "three".  I can easily ask for a match m/two.*three/ but how can i find out what the actual pattern that matched is?  how do i find out what's between two and three?  This really has me stumped.  Thanks!  

Posted by admin (Graham Ellis), 13 January 2003
If you put round brackets round the "interesting bits" that you want to extract, they'll be saved into the special variable $1.

Example:

Code:
[localhost:~/jan03] graham% cat mat
$text = "One two and three then four and finally five";
$text =~ /two(.*)three/;
print "We got $1 out of that!\n";
[localhost:~/jan03] graham% perl mat
We got  and  out of that!
[localhost:~/jan03] graham%


If you use more than one set of brackets, you'll also get $2, $3, etc ... and if you save the result of the match into a list, you can get the results into a named variable as well.

Add a "g" modifier after the final /, and you get a global match - in a scalar context it will give you the next match each time (use that in a loop), and in a list context it gives you  a list of all matches.

Perl is a very exciting language - lots of ways to do anything  



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

Comment: "Great answer for my question. Ranked high in my google ..."
Visitor Ranking 5.0 (5=excellent, 1=poor)
1 unpublished comment pending on this page

Comment by Anon (published 2010-05-18)
Great answer for my question. Ranked high in my google search. [#3573]

You can Add a comment or ranking or edit your own comments
There are presently 1 new comment(s) pending publication on this page

Average page ranking - 5.0

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho