473,396 Members | 2,013 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.

using regexp

hi
how can i use regexp to group these digits into groups of 3?

eg
line 123456789123456789

i have :

pat = re.compile("line\s+(\d{3})" , re.M|re.DOTALL)

but this only gives the first 3. I also tried

"line\s+(\d{3})+"
but also not working.
I need output to be ['123' ,'456','789', '123','456','789', .....]
thanks.

Mar 20 '07 #1
4 1196
You don't even need regex.

def
split_seq(seq,size):

# this is sort of the inverse of
flatten

# Source:
http://aspn.activestate.com/ASPN/Coo.../Recipe/425044

return [seq[i:i+size] for i in range(0, len(seq),
size)]

line =
'123456789123456789'

print
split_seq(line,3)

Will that work for you?

s9************@yahoo.com wrote:
hi
how can i use regexp to group these digits into groups of 3?

eg
line 123456789123456789

i have :

pat = re.compile("line\s+(\d{3})" , re.M|re.DOTALL)

but this only gives the first 3. I also tried

"line\s+(\d{3})+"
but also not working.
I need output to be ['123' ,'456','789', '123','456','789', .....]
thanks.

--
Shane Geiger
IT Director
National Council on Economic Education
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
Mar 20 '07 #2

import re
line = '123456789123456789'
print re.findall('([0-9]{3})', line)

Shane Geiger wrote:
You don't even need regex.

def
split_seq(seq,size):

# this is sort of the inverse of
flatten

# Source:
http://aspn.activestate.com/ASPN/Coo.../Recipe/425044

return [seq[i:i+size] for i in range(0, len(seq),
size)]

line =
'123456789123456789'

print
split_seq(line,3)

Will that work for you?

s9************@yahoo.com wrote:
>hi
how can i use regexp to group these digits into groups of 3?

eg
line 123456789123456789

i have :

pat = re.compile("line\s+(\d{3})" , re.M|re.DOTALL)

but this only gives the first 3. I also tried

"line\s+(\d{3})+"
but also not working.
I need output to be ['123' ,'456','789', '123','456','789', .....]
thanks.

--
Shane Geiger
IT Director
National Council on Economic Education
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
Mar 20 '07 #3
On Mar 20, 1:57 pm, Shane Geiger <sgei...@ncee.netwrote:
import re
line = '123456789123456789'
print re.findall('([0-9]{3})', line)

Shane Geiger wrote:
You don't even need regex.
def
split_seq(seq,size):
# this is sort of the inverse of
flatten
# Source:
http://aspn.activestate.com/ASPN/Coo.../Recipe/425044
return [seq[i:i+size] for i in range(0, len(seq),
size)]
line =
'123456789123456789'
print
split_seq(line,3)
Will that work for you?
s99999999s2...@yahoo.com wrote:
hi
how can i use regexp to group these digits into groups of 3?
eg
line 123456789123456789
i have :
pat = re.compile("line\s+(\d{3})" , re.M|re.DOTALL)
but this only gives the first 3. I also tried
"line\s+(\d{3})+"
but also not working.
I need output to be ['123' ,'456','789', '123','456','789', .....]
thanks.

--
Shane Geiger
IT Director
National Council on Economic Education
sgei...@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

sgeiger.vcf
1KDownload

hi
thanks.
I know it can be done without regexp, but its just for learning
purpose, i want to use regexp
eg
someword 123456789123456789 #this is a line of a file. 'someword' is a
word before the digits
I need to get the digits into groups of 3 based on whether i found
'someword'.
so i use this pattern :
"someword\s+(\d{3})"
but this will on give me the first 3. I need to group them all.
hope i explain it clearly.

Mar 20 '07 #4
On Mar 19, 10:33 pm, s99999999s2...@yahoo.com wrote:
hi
how can i use regexp to group these digits into groups of 3?

eg
line 123456789123456789

i have :

pat = re.compile("line\s+(\d{3})" , re.M|re.DOTALL)

but this only gives the first 3. I also tried

"line\s+(\d{3})+"
but also not working.
I need output to be ['123' ,'456','789', '123','456','789', .....]
thanks.


Try:

import re

target_string = """not 123456789 but
try this line 987654321"""

try:
digits = re.compile(r'line\s+(\d
+)').search(target_string).group(1)
except AttributeError:
digits = ''

three_at_a_time = re.compile(r'\d{3}').findall(digits)
print three_at_a_time

--
Hope this helps,
Steven

Mar 20 '07 #5

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

Similar topics

4
by: David | last post by:
Hi, I've had a search through google but couldn't really find the answer I was looking for.I'm new to PHP, so please take it <relatively> easy. I've created a script which runs some SNMP...
6
by: Lukas Holcik | last post by:
Hi Python crazies!:)) There is a problem to be solved. I have a text and I have to parse it using a lot of regular expressions. In (lin)u(ni)x I could write in bash: cat file | sed 's/../../' |...
5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
5
by: Ones Self | last post by:
Hi all: I'm trying to replace using a regexp read from a file: $string = '123 456 789'; # these two are usualy read from a file, # and so have to be in variables. $re = '()'; $rep = '|$1|';
0
by: Chris Croughton | last post by:
I'm trying to use the EXSLT regexp package from http://www.exslt.org/regexp/functions/match/index.html (specifically the match function) with the libxml xltproc (which supports EXSLT), but...
4
by: Jon Maz | last post by:
Hi All, I want to strip the accents off characters in a string so that, for example, the (Spanish) word "práctico" comes out as "practico" - but ignoring case, so that "PRÁCTICO" comes out as...
6
by: Jake Barnes | last post by:
This function has always worked for me just fine: function nl2br_js(myString){ // 02-18-06 - this function imitates the PHP command nl2br, which finds newlines in a string // and replaces them...
7
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
4
by: conan | last post by:
This regexp '<widget class=".*" id=".*">' works well with 'grep' for matching lines of the kind <widget class="GtkWindow" id="window1"> on a XML .glade file However that's not true for the...
2
by: X l e c t r i c | last post by:
Here: http://bigbangfodder.fileave.com/res/sandr.html I'm trying to use string.replace() for a basic search and replace form using textarea values as the regexp and replacement values for...
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: 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...
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...
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
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,...

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.