473,396 Members | 1,865 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,396 software developers and data experts.

RE exact match

26
I am having problem in re module of python.

when i do a call of match function it returns true if first of the expression is true.

For Eg :- Expression like ([a-z]+([+][a-z])*) to denote
(a-z)^* + (a-z)^* returns true for ab+ and ab+12

What is the correct method to match exact expression in re module of python ??

That is above 123 should not be accepted but it still is.........

Thanxx a lot !!
Jun 7 '10 #1
4 2712
bvdet
2,851 Expert Mod 2GB
If I understand your question correctly, the following expression will match 'ab+' and 'ab+df' but will not match 'ab+12':
Expand|Select|Wrap|Line Numbers
  1. patt = r'([a-z]+)[+]([a-z]*)$'
Jun 7 '10 #2
Glenton
391 Expert 256MB
These docs should help. I don't entirely understand your question, as it's not clear (to me at least) what is regular expressions and what is strings you're trying to match to!

But * means "0 or more of the preceding character(s)"
and + means "1 or more of the preceding character(s)"
Jun 7 '10 #3
nk28
26
What i want to know is if there 's a function in python that does exact matching of regular expression.

The docs say that the match function returns true if some part of it gets accepted.....

Thanxx a lot....
Jun 7 '10 #4
Glenton
391 Expert 256MB
The more detailed docs give you the answer, I think.

But @bvdet has got the answer as usual. The $ character matches the end of the string. So with the pattern as:

Expand|Select|Wrap|Line Numbers
  1. pat=r'([a-z]+)[+]([a-z]*)$'
  2.  
This returns a match:
Expand|Select|Wrap|Line Numbers
  1. In [3]: re.match(pat,"ab+cd")
  2. Out[3]: <_sre.SRE_Match object at 0x992d3c8>
  3.  
But these do not:
Expand|Select|Wrap|Line Numbers
  1. In [4]: re.match(pat,"ab+12")
  2.  
  3. In [5]: re.match(pat,"ab1"+"ab")
  4.  
  5. In [6]: re.match(pat,"ab+cd1234")
  6.  
Jun 8 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mark Findlay | last post by:
I am trying to figure out how to set up my reg exp search so that the search will only match on the exact word. Here is the current problem code: Word1 = "RealPlayer.exe" Word2 = "Player.exe"...
2
by: Andy | last post by:
Hi... i'm trying to understand the concept of function name overloading in c++. to understand the resolving system it's important to understand the diffrent levels of typecasting (exact match,...
2
by: Larry | last post by:
I have the following code attached to a search button on a form that runs a query. It works great, except that the search for Last Name only returns exact matches. It is even case sensitive....
0
by: NTPT | last post by:
Hi. i have table like this: create table my_data ( cond_1 int8,cond_2 varchar(),cond_3 .... cond_n whatrver ,data text) This table represents a simple tree structure with known max level...
3
by: baka | last post by:
Dear Sir/madam Here i am having some one sql which returns more rows than the required rows after comaparing tables simple sql statement will be lokk like SELECT t.empcd, s.daicd, s.chucd,...
2
by: Wayne Shu | last post by:
Hi everyone. In the following program, foo is an ambiguous call. #include <iostream> using namespace std; void foo(int *); void foo(int (&));
2
by: Slippy27 | last post by:
I'm trying to modify a find/replace script which iterates through a file A and makes replacements defined in a csv file B. My original goal was to change any line in file A containing a search string...
2
by: pankajit09 | last post by:
Hello, $x = "tyu" $y="3" $eq = "asa+tyu-tyut+1" $result = str_replace($x,$y,$eq);
6
by: Mr.SpOOn | last post by:
Hi, I'd like to use regular expressions to parse a string and accept only valid strings. What I mean is the possibility to check if the whole string matches the regex. So if I have: I can...
0
by: Lie Ryan | last post by:
On Sun, 26 Oct 2008 17:51:29 +0100, Mr.SpOOn wrote: re.compile('a*b*$') $ matches the end of a string, or in MULTILINE mode, the end of a line (right before newline) Symmetrically, ^...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.