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

Regular expression doubt

1
problem : atleast 2 char string wherein 1 char should be between a-k , second char should a number divisible by 3 and then any digit or character.

Expand|Select|Wrap|Line Numbers
  1. import re
  2. patt=re.compile(r"[a-k][0369][a-zA-Z0-9#]*")
  3. m=patt.search("aaa9#")
  4. if(m!=None):
  5.     print("Matched")
  6. else:
  7.     print("Not Matched")
Why does above program gives Matched?
May 9 '19 #1
3 1383
dwblas
626 Expert 512MB
There is an old saying, "you have to solve a problem. You decide to use regular expressions. Now you have two problems." So unless you are required to use regular expressions, it is easier and more straight forward to use if statements
Expand|Select|Wrap|Line Numbers
  1. ## atleast 2 char string
  2. test_string="aaa9#"
  3. if len(test_string > 1):
  4.     ## wherein 1 char should be between a-k
  5.     ## I assume this means the first character
  6.     if "a" <= test_string[0] <= "k":
  7.         ## second char should a number divisible by 3
  8.  
  9.         ## *****Your test string fails here*****
  10.         if test_string[1].isdigit() and 0 == int(test_string[1]) % 3:
  11.             print("Matched") 
May 9 '19 #2
Rabbit
12,516 Expert Mod 8TB
Because the substring "a9" matches your regex. Nothing in your regex says the whole string has to conform.
May 10 '19 #3
Luuk
1,047 Expert 1GB
@Rabbit is right, and maybe a bit 'off-topic', but yesterday I was reading this on stackoverflow. which might be of interest. it's about using regular expressions … 😉
May 12 '19 #4

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

Similar topics

4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
20
by: Asper Faner | last post by:
I seem to always have hard time understaing how this regular expression works, especially how on earth do people bring it up as part of computer programming language. Natural language processing...
16
by: Mark Rae | last post by:
Hi, Supposing I had a string made up of a person's name followed by their profession in parentheses e.g. string strText = "Tiger Woods (golfer)"; and I wanted to extract the portion of the...
12
by: =?Utf-8?B?SlA=?= | last post by:
I am a newbie to regular expressions and want to extract a number from the end of a string. The string would have these formats: image/4567 image/45678 image/456789 I would also want to...
7
by: Sharkie | last post by:
I need a regular expression which will evaluate to false if number of consecutive characters (non-whitespace) exceeds certain number (10 in this example). For example, I have this function: ...
9
by: Kirk | last post by:
Hi All, the following regular expression matching seems to enter in a infinite loop: ################ import re text = ' MSX INTERNATIONAL HOLDINGS ITALIA srl (di seguito MSX ITALIA) una '...
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: 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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: 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...

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.