473,403 Members | 2,338 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,403 software developers and data experts.

Problema con le RE....

Problema con le RE....
Ho questa stringa "3 HOURS, 22 MINUTES, and 28 SECONDS" e la devo
'dividere' nelle sue tre parti "3 HOURS", "22 MINUTES", "28 SECONDS".
La cosa mi viene molto con le RE...(inutile la premessa che sono molto
alle prime armi con RE e Python)
Qesito perchè se eseguo questo codice
regex=re.compile("[0-9]+ (HOUR|MINUTE|SECOND)")
print regex.findall("22 MINUTE, 3 HOUR, AND 28 SECOND") ottengo come output:
['MINUTE', 'HOUR', 'SECOND']
e non come mi aspettavo:
['3 MINUTE', '22 HOUR', '28 SECOND']


Saluti e grazie mille...
Alessandro

Jan 9 '06 #1
2 1269
Alessandro wrote:
Problema con le RE....
Ho questa stringa "3 HOURS, 22 MINUTES, and 28 SECONDS" e la devo
'dividere' nelle sue tre parti "3 HOURS", "22 MINUTES", "28 SECONDS".
La cosa mi viene molto con le RE...(inutile la premessa che sono molto
alle prime armi con RE e Python)
Qesito perchè se eseguo questo codice
>>>>regex=re.compile("[0-9]+ (HOUR|MINUTE|SECOND)")
>>>>print regex.findall("22 MINUTE, 3 HOUR, AND 28 SECOND") ottengo come output:
>>>> ['MINUTE', 'HOUR', 'SECOND']
e non come mi aspettavo:
>>>> ['3 MINUTE', '22 HOUR', '28 SECOND']
Saluti e grazie mille...
Alessandro

Would probably be slightly easier had you written it in english, but
basically the issue is the matching group.

A match group is defined by the parenthesis in the regular expression,
e.g. your match group is "(HOUR|MINUTE|SECOND)", which means that only
that will be returned by a findall.

You need to include the number as well, and you can use a non-grouping
match for the time (with (?: ) instead of () ) to prevent dirtying your
matched groups.
pattern = re.compile(r"([0-9]+ (?:HOUR|MINUTE|SECOND))")
Other improvements:
* \d is a shortcut for "any digit" and is therefore equivalent to [0-9]
yet slightly clearer.
* You may use the re.I (or re.IGNORECASE) to match both lower and
uppercase times
* You can easily handle an optional "s"

Improved regex:
pattern = re.compile(r"(\d+ (?:hour|minute|second)s?)", re.I)
pattern.findall("3 HOURS 22 MINUTES 28 SECONDS") ['3 HOURS', '22 MINUTES', '28 SECONDS'] pattern.findall("1 HOUR 22 MINUTES 28 SECONDS")

['1 HOUR', '22 MINUTES', '28 SECONDS']

If you want to learn more about regular expressions, I suggest you to
browse and read http://regular-expressions.info/ it's a good source of
informations, and use the Kodos software which is a quite good Python
regex debugger.
Jan 9 '06 #2
Thanks for the reply it's ok!!!
The language? I selected the wrong newsgroup in my
newsreader!!!...sorry...

Thanks...

Alessandro...

Jan 9 '06 #3

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

Similar topics

0
by: Sergio | last post by:
Olá pessoal Estou com um problema num relatório, quando mando imprimir ou visualizar um determinado relatório com um subrelatório que tem varios campos entre os quais quant, preço, total, fazendo...
0
by: StellaAzzurra | last post by:
Salve, premettendo che non sono un esperta di access, vi pongo il mio problema. Sto facendo una rubrica con access ed avrei la necessità di fare in modo che se ad esempio un mio clienti avesse 5...
1
by: Juan | last post by:
Hola Grupo! Tengo el siguiente problema. Quiero desarrollar una aplicación ASP.NET con Visual Studio.NET y resulta que cuando quiero crear una me aparece el siguiente mensaje ...
1
by: teo_teo_teo | last post by:
ciao, in un campo "file" di un documento xml ho qualcosa del tipo ..\folder\file.pdf ovviamente nn posso cambiare il modo in cui il folder e il file sono visualizzti in quel tag perché il file...
0
by: teo_teo_teo | last post by:
ciao, in un campo "file" di un documento xml ho qualcosa del tipo ..\folder\file.pdf ovviamente nn posso cambiare il modo in cui il folder e il file sono visualizzti in quel tag perché il file...
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:
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
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
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
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
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
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...

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.