finding the list of the matched strings

J

jm.suresh

Hi, I have a list of strings. And I want to find the subset which
matches a particular regular expression.

import re
ll = ('a', 'b', 's1', 's2', '3s')
p = re.compile('^s.*')
newList = filter(lambda s: p.match(s), ll)

I suppose there should be simple function to do this in re module. Is
there any?

I searched google, but could not find one, may be for keywords were not
perfect.

Thanks.
Suresh
 
J

James Stroud

Hi, I have a list of strings. And I want to find the subset which
matches a particular regular expression.

import re
ll = ('a', 'b', 's1', 's2', '3s')
p = re.compile('^s.*')
newList = filter(lambda s: p.match(s), ll)

I suppose there should be simple function to do this in re module. Is
there any?

I searched google, but could not find one, may be for keywords were not
perfect.

Thanks.
Suresh

I think I'm having some network problems. I'll try again. Also the
previous "attempt" had a typo (perhaps a freudian slip).

",".join(some_list).

By the way, don't name your own objects with the names of built-in
types, such as "list" or "str", etc.
 
G

Gerard Flanagan

Hi, I have a list of strings. And I want to find the subset which
matches a particular regular expression.

import re
ll = ('a', 'b', 's1', 's2', '3s')
p = re.compile('^s.*')
newList = filter(lambda s: p.match(s), ll)

I suppose there should be simple function to do this in re module. Is
there any?

#>>> s = ('a', 'b', 's1', 's2', 'c')
#>>> import re
#>>> regexp = re.compile('^s.*')
#>>> filter( regexp.match, s)
#('s1', 's2')
#>>>

Gerard
 
C

Christian Joergensen

Hi, I have a list of strings. And I want to find the subset which
matches a particular regular expression.

import re
ll = ('a', 'b', 's1', 's2', '3s')
p = re.compile('^s.*')
newList = filter(lambda s: p.match(s), ll)

I suppose there should be simple function to do this in re module. Is
there any?

I searched google, but could not find one, may be for keywords were not
perfect.

I dont believe there exists such a function. I would have written
it using a list comprehension.
import re
ll = ('a', 'b', 's1', 's2', '3s')
p = re.compile('^s.*')
newList = [s for s in ll if p.match(s)]
newList
['s1', 's2']
 
P

Peter Otten

Hi, I have a list of strings. And I want to find the subset which
matches a particular regular expression.

import re
ll = ('a', 'b', 's1', 's2', '3s')
p = re.compile('^s.*')
newList = filter(lambda s: p.match(s), ll)

or

newList = [s for s in ll if p.match(s)]
I suppose there should be simple function to do this in re module. Is
there any?

No.

Peter
 
J

John Machin

James Stroud wrote:
[snip]
I think I'm having some network problems. I'll try again. Also the
previous "attempt" had a typo (perhaps a freudian slip).

Also this time you replied to the wrong thread :)
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top