regular expression : the dollar sign ($) work with re.match() or re.search() ?

Discussion in 'Python' started by iMath, Sep 26, 2012.

  1. iMath

    iMath Guest

    I only know the dollar sign ($) will match a pattern from the
    end of a string,but which method does it work with ,re.match() or re.search() ?
    iMath, Sep 26, 2012
    #1
    1. Advertising

  2. Re: regular expression : the dollar sign ($) work with re.match() or re.search()

    iMath writes:

    > I only know the dollar sign ($) will match a pattern from the end of
    > a string, but which method does it work with, re.match() or
    > re.search()


    It works with both. With re.match, the pattern has to match at the
    start of the string _and_ the $ has to match the end of the string (or
    a line); re.search scans the string until it finds a suitable start.

    What was the weird character that you used as a question mark? I
    removed them because they confuse the newsreader I use.
    Jussi Piitulainen, Sep 26, 2012
    #2
    1. Advertising

  3. Re: regular expression : the dollar sign ($) work with re.match() orre.search()

    On Wed, Sep 26, 2012 at 5:48 PM, Jussi Piitulainen
    <> wrote:
    > What was the weird character that you used as a question mark? I
    > removed them because they confuse the newsreader I use.


    It appears to be Unicode Character 'FULLWIDTH QUESTION MARK' (U+FF1F).
    Normally I'd be inclined to simply use U+003F instead, but hey, it's a
    question mark still.

    ChrisA
    Chris Angelico, Sep 26, 2012
    #3
  4. iMath

    Peter Otten Guest

    Re: regular expression : the dollar sign ($) work with re.match() orre.search() ?

    iMath wrote:

    > I only know the dollar sign ($) will match a pattern from the
    > end of a string,but which method does it work with ,re.match() or
    > re.search() ?


    Why not try it out in the interactive interpreter? Here's the "deluxe
    version":

    >>> def demo(pattern="mid$", texts=["start mid end", "start mid", "mid end",

    "mid"], matchers=[re.match, re.search]):
    .... print "pattern:", pattern
    .... for text in texts:
    .... for matcher in matchers:
    .... name = matcher.__name__
    .... print u"\N{CHECK MARK}" + name if matcher(pattern,
    text) else (" "*(len(name)+1)),
    .... print repr(text)
    ....
    >>> demo()

    pattern: mid$
    'start mid end'
    ✓search 'start mid'
    'mid end'
    ✓match ✓search 'mid'
    Peter Otten, Sep 26, 2012
    #4
  5. Re: regular expression : the dollar sign ($) work with re.match() or re.search()

    Alister writes:
    > On Wed, 26 Sep 2012 10:48:00 +0300, Jussi Piitulainen wrote:
    >
    > > iMath writes:
    > >
    > >> I only know the dollar sign ($) will match a pattern from the end
    > >> of a string, but which method does it work with, re.match() or
    > >> re.search()

    > >
    > > It works with both. With re.match, the pattern has to match at the
    > > start of the string _and_ the $ has to match the end of the string
    > > (or a line); re.search scans the string until it finds a suitable
    > > start.
    > >
    > > What was the weird character that you used as a question mark? I
    > > removed them because they confuse the newsreader I use.

    >
    > It shows fine in my news reader, perhaps you should consider changing to
    > one that works properly (btw I am using pan on a fedora 17 netbook)


    I was just curious why anyone would use anything other than the ASCII
    question mark as an ordinary question mark when writing in English in
    a newsgroup.

    The post had this:

    0000520 61 72 63 68 28 29 20 20 ef bc 9f 0a
    a r c h ( ) sp sp o < us nl

    od is showing (ef bc 9f) as (o < us) but since they are not individual
    characters anyway, never mind that. Google tells me (ef bc 9f) is
    UTF-8 for U+FF1F FULLWIDTH QUESTION MARK, so now I basically have my
    answer as to what it is, though still not as to why one would use it.

    The ordinary question mark would look like this:

    0000000 61 72 63 68 28 29 20 3f 0a
    a r c h ( ) sp ? nl
    Jussi Piitulainen, Sep 26, 2012
    #5
  6. iMath

    Ian Kelly Guest

    Re: regular expression : the dollar sign ($) work with re.match() or re.search() ?

    On Fri, Sep 28, 2012 at 12:07 PM, Prasad, Ramit
    <> wrote:
    > I guess you can consider re.match's pattern to be
    > prefixed with '^'.


    You can in this case, but they're not equivalent in multi-line mode:

    >>> re.match('^two', 'one\ntwo', re.M)
    >>> re.search('^two', 'one\ntwo', re.M)

    <_sre.SRE_Match object at 0x0475BFA8>
    Ian Kelly, Sep 28, 2012
    #6
  7. iMath

    iMath Guest

    在 2012å¹´9月26日星期三UTC+8下åˆ3æ—¶38分50秒,iMath写é“:
    > I only know the dollar sign ($) will match a pattern from the
    >
    > end of a string,but which method does it work with ,re.match() or re.search() ?


    I thought re.match('h.$', 'hbxihi') will match ‘hi’ ,but itdoes not .so why ?
    iMath, Jan 7, 2013
    #7
  8. Re: regular expression : the dollar sign ($) work with re.match()or re.search() $)C#?

    On Mon, 07 Jan 2013 01:45:58 -0800, iMath wrote:

    > 在 2012å¹´9月26日星期三UTC+8下åˆ3æ—¶38分50秒,iMath写é“:
    >> I only know the dollar sign ($) will match a pattern from the
    >>
    >> end of a string,but which method does it work with ,re.match() or
    >> re.search() ?

    >
    > I thought re.match('h.$', 'hbxihi') will match ‘hi’ ,but it does not .so
    > why ?



    re.match only matches at the *start* of the string, so "h.$" tries to
    match:

    * start of string
    * literal h
    * any character
    * end of string


    You want re.search, which will search the entire string and match "hi" at
    the end of the string.


    --
    Steven
    Steven D'Aprano, Jan 7, 2013
    #8
    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. Norman Yuan
    Replies:
    0
    Views:
    857
    Norman Yuan
    Dec 8, 2005
  2. Marcus Leon
    Replies:
    1
    Views:
    3,027
    Matt Humphrey
    Jun 7, 2006
  3. Mark Hahn
    Replies:
    29
    Views:
    1,030
    Fredrik Lundh
    Apr 21, 2004
  4. belred

    u'\N{dollar sign}'

    belred, May 21, 2009, in forum: Python
    Replies:
    1
    Views:
    368
    John Machin
    May 21, 2009
  5. Dennis Walter

    Why use dollar sign $ for variables

    Dennis Walter, Jul 6, 2004, in forum: Perl Misc
    Replies:
    21
    Views:
    309
    Ian Wilson
    Jul 14, 2004
Loading...

Share This Page