473,508 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

finding the list of the matched strings

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

Nov 5 '06 #1
5 939
"jm*******@no.spam.gmail.com" <jm*******@gmail.comwrites:
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']

--
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk | Visit us at: http://www.gmta.info
Nov 5 '06 #2
jm*******@no.spam.gmail.com wrote:
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.
Nov 5 '06 #3

jm*******@no.spam.gmail.com wrote:
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

Nov 5 '06 #4
jm*******@no.spam.gmail.com wrote:
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
Nov 5 '06 #5

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 :-)
>
",".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.
Nov 5 '06 #6

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

Similar topics

3
1501
by: Jacob Weber | last post by:
If I do a search with a regular expression that uses parenthesized subexpressions, is there a way to find out where it found the subexpressions? For example, say I do: "bb".search(/b(.)/g); ...
2
3283
by: B Moor | last post by:
I have a database with 100,000's records, each with a unique reference, eg A123BNK456 I would like to generate a search facility whereby we can choose an exact match or partial match, where the...
19
13543
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
5
5734
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
8
2569
by: Bob | last post by:
I need to create a Regex to extract all strings (including quotations) from a C# or C++ source file. After being unsuccessful myself, I found this sample on the internet: ...
40
2688
by: nufuhsus | last post by:
Hello all, First let me appologise if this has been answered but I could not find an acurate answer to this interesting problem. If the following is true: C:\Python25\rg.py>python Python...
2
1533
by: dayanand ramasubbu | last post by:
Hi, I want to get the best possible match for a strings among a list of strings. For eg, my string to be matched is hi/howareyou?rudoingok which needs to be matched against the below list, ...
275
12038
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
4
1065
by: Ben | last post by:
Hello All: I am new to Python, and I love it!! I am running 2.6 on Windows. I have a flat text file here is an example of 3 lines with numbers changed for security: ...
0
7231
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
7132
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
7336
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,...
1
7063
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
7504
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
3211
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
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.