regular expressions help

Pradeep Patra smilesonisamal at gmail.com
Thu Sep 19 05:34:12 EDT 2019


Thanks David for your quick help. Appreciate it. When I tried on python
2.7.3 the same thing you did below I got the error after matches.group(0)
as follows:

AttributeError: NoneType object has no attribute 'group'.

I tried to check 'None' for no match for re.search as the documentation
says but it's not working.

Unfortunately I cannot update the python version now to 2.7.13 as other
programs are using this version and need to test all and it requires more
testing. Any idea how I can fix this ? I am ok to use any other re
method(not only tied to re.search) as long as it works.

On Thursday, September 19, 2019, David <bouncingcats at gmail.com> wrote:

> On Thu, 19 Sep 2019 at 18:41, Pradeep Patra <smilesonisamal at gmail.com>
> wrote:
> > On Thursday, September 19, 2019, Pradeep Patra <smilesonisamal at gmail.com>
> wrote:
> >> On Thursday, September 19, 2019, David <bouncingcats at gmail.com> wrote:
> >>> On Thu, 19 Sep 2019 at 17:51, Pradeep Patra <smilesonisamal at gmail.com>
> wrote:
>
> >>> > pattern=re.compile(r'^my\-dog$')
> >>> > matches = re.search(mystr)
>
> >>> > In the above example both cases(match/not match) the matches returns
> "None"
>
> >>> Hi, do you know what the '^' character does in your pattern?
>
> >> Beginning of the string. But I tried removing that as well and it still
> could not find it. When I tested at www.regex101.com and it matched
> successfully whereas I may be wrong. Could you please help here?
>
> > I am using python 2.7.6 but I also tried on python 3.7.3.
>
> $ python2
> Python 2.7.13 (default, Sep 26 2018, 18:42:22)
> [GCC 6.3.0 20170516] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import re
> >>> mystr= "where is my-dog"
> >>> pattern=re.compile(r'my-dog$')
> >>> matches = re.search(mystr)  # this is syntax error, but it is what you
> showed above
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: search() takes at least 2 arguments (1 given)
> >>> matches = re.search(pattern, mystr)
> >>> matches.group(0)
> 'my-dog'
> >>>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list