[perl-python] string pattern matching

Discussion in 'Perl Misc' started by Xah Lee, Feb 1, 2005.

  1. Xah Lee

    Xah Lee Guest

    # -*- coding: utf-8 -*-
    # Python

    # Matching string patterns
    #
    # Sometimes you want to know if a string is of
    # particular pattern. Let's say in your website
    # you have converted all images files from gif
    # format to png format. Now you need to change the
    # html code to use the .png files. So, essentially
    # you want to replace strings of the form
    #
    # img src="*.gif"
    # to
    # img src="*.png"
    #
    # Python provides string pattern matching by the
    # "re" module. (String Pattern Matching is
    # inanely known as Regular Expression (regex) in
    # the computing industry. It is a misnomer and
    # causes great unnecessary confusion.)


    ©import re
    ©
    ©text='''<img src="../Icons_dir/icon_sum.gif" width="32"
    height="32">'''
    ©
    ©pattern = r'''src="([^"]+)\.gif"'''
    ©
    ©result = re.search(pattern, text)
    ©
    ©if result:
    © print 'yes'
    © print result.expand(r"\1") # the captured pattern
    ©else:
    © print 'no'

    #-------------
    # if re.search(pattern, text) does not match, then the result is
    None. If it matches, an object is returned. One can then use methods
    such as .groups(), .expand(), .split() ... to find the matched parts,
    or a given replacement string, or split the text into multiple part
    by the regex...

    # regex is quite confusing for beginners, but
    # isn't really difficult to understand. It comes
    # with practice.

    # see
    # http://python.org/doc/lib/module-re.html
    # for detail.

    # i'll have to study more to make complet example. Try it yourself.

    -----------------

    # in perl, regex is its mainstay.
    # very expressive with syntax, but not so in semantic,
    # when compared to Python.

    # for syntax variability, for example the following are all the same.

    $text = "what ever is here to be matched";

    if ( $text =~ ever) { print 'yes'} else {print "no"}
    if ( $text =~ /ever/) { print 'yes'} else {print "no";}
    if ( $text =~ m/ever/) { print 'yes'} else {print "no"}
    if ( $text =~ m(ever)) { print 'yes'} else {print "no"}
    if ( $text =~ m@ever@) { print 'yes'} else {print "no"}

    # for detail of its much ado about nothing nature,
    # see perldoc perlre

    --------
    this is perl-python a-day mailing list. To subscribe, see
    http://xahlee.org/perl-python/python.html
    Xah

    http://xahlee.org/PageTwo_dir/more.html
     
    Xah Lee, Feb 1, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee
    Replies:
    9
    Views:
    878
    Chris Smith
    Feb 2, 2005
  2. Xah Lee
    Replies:
    4
    Views:
    637
    Aaron Sherman
    Feb 11, 2005
  3. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    260
    Marc Bissonnette
    Jan 13, 2004
  4. Xah Lee
    Replies:
    1
    Views:
    204
    John Bokma
    Feb 7, 2005
  5. Bobby Chamness
    Replies:
    2
    Views:
    263
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page