Regexps and lists

P

Paddy

I don't know enough to write an R.E. engine so forgive me if I am
being naive.
I have had to atch text involving lists in the past. These are usually
comma separated words such as
"egg,beans,ham,spam,spam"
you can match that with:
r"(\w+)(,\w+)*"
and when you look at the groups you get the following

Notice how you only get the last match as the second groups value.

It would be nice if a repeat operator acting on a group turned that
group into a sequence returning every match, in order. (or an empty
sequence for no matches).

The above exaple would become:

1, Is it possible? do any other RE engines do this?
2, Should it be added to Python?

- Paddy.
 
J

John Machin

I don't know enough to write an R.E. engine so forgive me if I am
being naive.
I have had to atch text involving lists in the past. These are usually
comma separated words such as
"egg,beans,ham,spam,spam"
you can match that with:
r"(\w+)(,\w+)*"

You *can*, but why do that? What are you trying to achieve? What is
the point of distinguishing the first element from the remainder?

See if any of the following do what you want:

| >>> s = "egg,beans,ham,spam,spam"
| >>> s.split(',')
| ['egg', 'beans', 'ham', 'spam', 'spam']
| >>> import re
| >>> re.split(r",", s)
| ['egg', 'beans', 'ham', 'spam', 'spam']
| >>> re.split(r"(,)", s)
| ['egg', ',', 'beans', ',', 'ham', ',', 'spam', ',', 'spam']
and when you look at the groups you get the following
('egg', ',spam')

Notice how you only get the last match as the second groups value.

It would be nice if a repeat operator acting on a group turned that
group into a sequence returning every match, in order. (or an empty
sequence for no matches).

The above exaple would become:


('egg', ('beans', 'ham', 'spam', ',spam'))

And then what are you going to do with the answer? Something like
this, maybe:

| >>> actual_answer = ('egg', ('beans', 'ham', 'spam', ',spam'))
| >>> [actual_answer[0]] +list(actual_answer[1])
| ['egg', 'beans', 'ham', 'spam', ',spam']

1, Is it possible?

Maybe, but I doubt the utility ...
do any other RE engines do this?

If your Google is not working, then mine isn't either.
2, Should it be added to Python?

No.

HTH,

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top