Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #74028
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2014-07-06 09:01 -0700 |
| References | <f949d1c4-98c4-417f-9abc-58fe72b4c0a0@googlegroups.com> |
| Message-ID | <34cc50af-9a3f-4739-bab4-55e4e321cf9a@googlegroups.com> (permalink) |
| Subject | Re: Why is it different from the example on the tutorial? |
| From | Rustom Mody <rustompmody@gmail.com> |
On Sunday, July 6, 2014 5:43:55 PM UTC+5:30, rxj...@gmail.com wrote:
> Hi,
> I type the following sample codes on Python, but it echoes differently.
> Regular expressions are compiled into pattern objects, which have methods for
> various operations such as searching for pattern matches or performing string
> substitutions.
> >>> import re
> >>> p = re.compile('ab*')
> >>> p
> What I get on Python console:
> $ python
> Python 2.7.5 (default, Oct 2 2013, 22:34:09)
> [GCC 4.8.1] on cygwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import re
> >>> p = re.compile('ab*')
> File "<stdin>", line 1
> p = re.compile('ab*')
> ^
> SyntaxError: invalid syntax
1. For *using* regular exps match is fine
For *hacking in the interpreter* I find findall more convenient.
match and findall take the same arguments
2. I wouldn't bother with compile at least at the start
3. Use raw strings for patterns even if it does not seem necessary (below)
$ python
Python 2.7.7 (default, Jun 3 2014, 16:16:56)
[GCC 4.8.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from re import findall
>>> findall(r'ab*', 'abcd')
['ab']
>>> findall(r'ab*', 'abcdab')
['ab', 'ab']
>>> findall(r'ab*', 'abcdabbb')
['ab', 'abbb']
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Why is it different from the example on the tutorial? rxjwg98@gmail.com - 2014-07-06 05:13 -0700
Re: Why is it different from the example on the tutorial? Tim Chase <python.list@tim.thechases.com> - 2014-07-06 07:54 -0500
Re: Why is it different from the example on the tutorial? rxjwg98@gmail.com - 2014-07-06 06:38 -0700
Re: Why is it different from the example on the tutorial? Rick Johnson <rantingrickjohnson@gmail.com> - 2014-07-06 07:18 -0700
Re: Why is it different from the example on the tutorial? Roy Smith <roy@panix.com> - 2014-07-06 10:34 -0400
Re: Why is it different from the example on the tutorial? Chris Angelico <rosuav@gmail.com> - 2014-07-07 00:45 +1000
Re: Why is it different from the example on the tutorial? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-06 15:47 +0100
Re: Why is it different from the example on the tutorial? Rick Johnson <rantingrickjohnson@gmail.com> - 2014-07-06 08:53 -0700
Re: Why is it different from the example on the tutorial? Roy Smith <roy@panix.com> - 2014-07-06 12:01 -0400
Re: Why is it different from the example on the tutorial? rxjwg98@gmail.com - 2014-07-06 08:03 -0700
Re: Why is it different from the example on the tutorial? Peter Otten <__peter__@web.de> - 2014-07-06 17:46 +0200
Re: Why is it different from the example on the tutorial? Rick Johnson <rantingrickjohnson@gmail.com> - 2014-07-06 09:02 -0700
Re: Why is it different from the example on the tutorial? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-06 19:29 +0100
Re: Why is it different from the example on the tutorial? Larry Hudson <orgnut@yahoo.com> - 2014-07-06 13:32 -0700
Re: Why is it different from the example on the tutorial? rxjwg98@gmail.com - 2014-07-06 13:42 -0700
Re: Why is it different from the example on the tutorial? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-06 15:25 +0100
Re: Why is it different from the example on the tutorial? Rustom Mody <rustompmody@gmail.com> - 2014-07-06 09:01 -0700
csiph-web