473,466 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Regular Expressions: Get number of matches

Xx r3negade
39 New Member
How do you return the number of times a certain pattern has been matched in a string?
May 28 '08 #1
4 9582
kaarthikeyapreyan
107 New Member
Hey try this it would match the pattern for the string given

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/env python
  2. ### Program to match the given sequence in a word
  3.  
  4. def pattern():
  5.   """
  6.   Search for a pattern in a string
  7.   """
  8.   count = 0
  9.   rex=raw_input("Enter the pattern : ")
  10.   mystr=raw_input("Enter the string : ")
  11.   patlen = len(rex)
  12.   word_split=group(patlen,mystr)
  13.  
  14.   for j in word_split :
  15.     if j == rex :
  16.       count = count + 1
  17.  
  18.   if rex in word_split :
  19.     print "Pattern Found %d time(s)"%count 
  20.   else :
  21.     print "Pattern not found"
  22.  
  23. def group(exprlength,word):
  24.   """
  25.   group characters for a given length
  26.   """
  27.   strlist = []
  28.   i = 0
  29.   for count in word :
  30.     relpat = word[i:i+exprlength]
  31.     if len(relpat) == exprlength:
  32.       strlist.append(relpat)
  33.       i = i +1
  34.   return strlist
  35.  
  36. pattern()
May 29 '08 #2
Smygis
126 New Member
You use the len functon.

Expand|Select|Wrap|Line Numbers
  1. >>> import re
  2. >>> txt = open("mess.text").read()
  3. >>> len(txt)
  4. 101250
  5. >>> rexp = re.compile(r"regexp here*")
  6. >>> match = rexp.findall(txt)
  7. >>> len(match)
  8. 10
  9.  
* Censored due to being the sulution for a puzzle in pythonchallange. :)

w00t, no python highlighting for the code?
May 29 '08 #3
jlm699
314 Contributor
w00t, no python highlighting for the code?
This has been an issue for some time now. Nobody has acknowledged it aside from one of the Moderators. I miss the python highlights :(

P.S. If a moderator is reading this do you have any idea why this is happening or could you contact an admin and try to figure out when it will be back?
May 29 '08 #4
Laharl
849 Recognized Expert Contributor
They're discussing it here.
May 29 '08 #5

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

Similar topics

8
by: Eric Linders | last post by:
Hi, I'm trying to figure out the most efficient method for taking the first character in a string (which will be a number), and use it as a variable to check to see if the other numbers in the...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
1
by: André Søreng | last post by:
With the re/sre module included with Python 2.4: pattern = "(?P<id1>avi)|(?P<id2>avi|mp3)" string2match = "some string with avi in it" matches = re.finditer(pattern, string2match) .......
2
by: John Smith | last post by:
Hello all: I was wondering if someone had a good regular expression for a phone number. I came up w/on that does validation to the point of having an optional area code, but I would like to...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
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...
3
by: Chris | last post by:
Hi everyone, I'm trying to parse through the contents of some text files with regular expressions, but am new to regular expressions and how to use them in VB.net. I'm pretty sure that the...
7
by: G .Net | last post by:
Hi I was wondering if you could help me with the following: I have a string e.g. "#54x454#,#22b6#,#885333#". I want to be able to store in an array list, the following from the string: ...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
14
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.