Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #30152 > unrolled thread

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

Started byiMath <redstone-cold@163.com>
First post2012-09-26 00:38 -0700
Last post2013-01-07 09:54 +0000
Articles 10 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  regular expression :  the dollar sign ($) work with re.match() or re.search()  ? iMath <redstone-cold@163.com> - 2012-09-26 00:38 -0700
    Re: regular expression :  the dollar sign ($) work with re.match() or re.search() Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-09-26 10:48 +0300
      Re: regular expression : the dollar sign ($) work with re.match() or re.search() Chris Angelico <rosuav@gmail.com> - 2012-09-26 17:57 +1000
      Re: regular expression :  the dollar sign ($) work with re.match() or re.search() Alister <alister.ware@ntlworld.com> - 2012-09-26 15:47 +0000
        Re: regular expression :  the dollar sign ($) work with re.match() or re.search() Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-09-26 19:29 +0300
    Re: regular expression : the dollar sign ($) work with re.match() or re.search() ? Peter Otten <__peter__@web.de> - 2012-09-26 10:33 +0200
    RE: regular expression :  the dollar sign ($) work with re.match() or re.search()  ? "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-09-28 18:07 +0000
    Re: regular expression : the dollar sign ($) work with re.match() or re.search() ? Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-28 12:32 -0600
    Re: regular expression :  the dollar sign ($) work with re.match() or re.search()  ? iMath <redstone-cold@163.com> - 2013-01-07 01:45 -0800
      Re: regular expression :  the dollar sign ($) work with re.match() or re.search()  � Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-07 09:54 +0000

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

FromiMath <redstone-cold@163.com>
Date2012-09-26 00:38 -0700
Subjectregular expression : the dollar sign ($) work with re.match() or re.search() ?
Message-ID<5d913584-b5ea-4ff1-8094-f4d5b273c0b6@googlegroups.com>
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()  ?

[toc] | [next] | [standalone]


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

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2012-09-26 10:48 +0300
SubjectRe: regular expression : the dollar sign ($) work with re.match() or re.search()
Message-ID<qotsja58btb.fsf@ruuvi.it.helsinki.fi>
In reply to#30152
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.

[toc] | [prev] | [next] | [standalone]


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

FromChris Angelico <rosuav@gmail.com>
Date2012-09-26 17:57 +1000
SubjectRe: regular expression : the dollar sign ($) work with re.match() or re.search()
Message-ID<mailman.1400.1348646287.27098.python-list@python.org>
In reply to#30155
On Wed, Sep 26, 2012 at 5:48 PM, Jussi Piitulainen
<jpiitula@ling.helsinki.fi> 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

[toc] | [prev] | [next] | [standalone]


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

FromAlister <alister.ware@ntlworld.com>
Date2012-09-26 15:47 +0000
SubjectRe: regular expression : the dollar sign ($) work with re.match() or re.search()
Message-ID<CsF8s.157208$356.82477@fx07.am4>
In reply to#30155
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)



-- 
Q: Why is Microsoft's Product Support a failure?
A: Because Microsoft needs a Support Group instead.

[toc] | [prev] | [next] | [standalone]


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

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2012-09-26 19:29 +0300
SubjectRe: regular expression : the dollar sign ($) work with re.match() or re.search()
Message-ID<qot7grgd9xc.fsf@ruuvi.it.helsinki.fi>
In reply to#30217
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

[toc] | [prev] | [next] | [standalone]


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

FromPeter Otten <__peter__@web.de>
Date2012-09-26 10:33 +0200
SubjectRe: regular expression : the dollar sign ($) work with re.match() or re.search() ?
Message-ID<mailman.1406.1348648406.27098.python-list@python.org>
In reply to#30152
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'

[toc] | [prev] | [next] | [standalone]


#30421

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2012-09-28 18:07 +0000
Message-ID<mailman.1576.1348855696.27098.python-list@python.org>
In reply to#30152
iMath wrote:
> Sent: Wednesday, September 26, 2012 2:39 AM
> To: python-list@python.org
> Subject: regular expression : the dollar sign ($) work with re.match() or re.search() ?
> 
> 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()  ?


You can try this on the interactive interpreter.
>>> re.match('hi$', 'xihi')
>>> re.search('hi$', 'xihi')
<_sre.SRE_Match object at 0x13FF7100>

Although, I think match does not work since match
only starts searching at the start of the string
while search looks for the pattern anywhere in the string.

>>> re.match('x.hi$', 'xihi')
<_sre.SRE_Match object at 0x15693BF0>

I guess you can consider re.match's pattern to be 
prefixed with '^'.

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

[toc] | [prev] | [next] | [standalone]


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

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-09-28 12:32 -0600
SubjectRe: regular expression : the dollar sign ($) work with re.match() or re.search() ?
Message-ID<mailman.1579.1348857160.27098.python-list@python.org>
In reply to#30152
On Fri, Sep 28, 2012 at 12:07 PM, Prasad, Ramit
<ramit.prasad@jpmorgan.com> 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>

[toc] | [prev] | [next] | [standalone]


#36334

FromiMath <redstone-cold@163.com>
Date2013-01-07 01:45 -0800
Message-ID<cd48c34f-f9f5-4fef-9342-5f601dbf0454@googlegroups.com>
In reply to#30152
在 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 ?

[toc] | [prev] | [next] | [standalone]


#36338 — Re: regular expression : the dollar sign ($) work with re.match() or re.search() �

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-01-07 09:54 +0000
SubjectRe: regular expression : the dollar sign ($) work with re.match() or re.search() �
Message-ID<50ea9b4f$0$21851$c3e8da3$76491128@news.astraweb.com>
In reply to#36334
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web