473,382 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Question on re.IGNORECASE

Hi,

I'm having some problems with basic RE in python. I was wondering
whether
somebody could provide a hint on what's going wrong with the following
script. Comments are included.

TIA.
-myself
python2.3

Python 2.3.4 (#1, Nov 18 2004, 13:39:30)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import re
pattern = re.compile('.*HTTP/(\d\.\d) *(\d*) *(.*)$')
pattern.search("GGHTTP/1.1 200 OK\r\n", re.IGNORECASE) <_sre.SRE_Match object at 0xb75c6ed0> pattern.search("GHTTP/1.1 200 OK\r\n", re.IGNORECASE) # this makes no sense to me. Why is the previous line matched
# and this not? pattern.search("GHTTP/1.1 200 OK\r\n")

<_sre.SRE_Match object at 0xb758d020>

Oct 20 '05 #1
3 3295
ch****@gmail.com wrote:
Hi,

I'm having some problems with basic RE in python. I was wondering
whether
somebody could provide a hint on what's going wrong with the following
script. Comments are included.

TIA.
-myself

python2.3
Python 2.3.4 (#1, Nov 18 2004, 13:39:30)
[GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-39)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import re
pattern = re.compile('.*HTTP/(\d\.\d) *(\d*) *(.*)$')
pattern.search("GGHTTP/1.1 200 OK\r\n", re.IGNORECASE)
<_sre.SRE_Match object at 0xb75c6ed0>
pattern.search("GHTTP/1.1 200 OK\r\n", re.IGNORECASE)
# this makes no sense to me. Why is the previous line matched


Because the second argument to pattern.search() is the position where the search starts, not a flag. re.IGNORECASE == 2 so your search is skipping the first two chars.

Try giving the flags as a second argument to re.compile():
import re
re.IGNORECASE 2 pattern = re.compile('.*HTTP/(\d\.\d) *(\d*) *(.*)$', re.IGNORECASE)
pattern.search("GGHTTP/1.1 200 OK\r\n") <_sre.SRE_Match object at 0x00965980> pattern.search("GHTTP/1.1 200 OK\r\n") <_sre.SRE_Match object at 0x009659D0> pattern.search("Ghttp/1.1 200 OK\r\n")
<_sre.SRE_Match object at 0x009651B0>

Kent

# and this not?
pattern.search("GHTTP/1.1 200 OK\r\n")


<_sre.SRE_Match object at 0xb758d020>

Oct 20 '05 #2
OK, I got it.

- The re module search function syntax is:

search( pattern, string[, flags])

where re.IGNORECASE is a valid flag.

- The RE Object search method syntax is:

search( string[, pos[, endpos]])

where "The optional second parameter pos gives an index in the string
where the search is to start; it defaults to 0"

It turns out that re.IGNORECASE has the value
2.

Am I the only person bitten by this?

http://docs.python.org/lib/node115.html
http://docs.python.org/lib/re-objects.html

-myself

Oct 20 '05 #3
Thanks for your help.

-myself

Oct 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Miguel Orrego | last post by:
Hi, I have a variable in an app called GenericTitle which contains text, a persons job title funnily enough. I want to check whether this variable contains the word "director" and if it does,...
1
by: Jeremy | last post by:
I am using regular expressions and I would like to use both re.IGNORECASE and re.VERBOSE options. I want to do something like the following (which doesn't work): matsearch = r'''^\ {0,4}(\d+)...
4
by: engwar1 | last post by:
Not sure where to ask this. Please suggest another newsgroup if this isn't the best place for this question. I'm new to both vb.net and regex. I need a regular expression that will validate what...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
2
by: Prabhu Gurumurthy | last post by:
Here is a simple program, which queries /var/log/daemon on my OpenBSD box and gets the list of valid ntp peers. Questions: what is the easiest way for me to create lists on the fly, by that I...
1
by: brainstorm | last post by:
Hi, I'm using Borland Delphi .NET 2005 and I've got a problem using Regular Expressions. But the RegExOptions do not work properly. My Replace - function looks like: function...
4
by: looping | last post by:
Hi, It's not really a Python question but I'm sure someone could help me. When I use RE, I always have trouble with this kind of search: Ex. I've a text file: """ create or replace...
0
by: Yann Le Boulanger | last post by:
Hi all, I have a problem with regex , utf-8 chars and IGNORECASE <_sre.SRE_Match object at 0x2aaaaed0c100> Here everything is ok.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.