469,902 Members | 1,944 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,902 developers. It's quick & easy.

re.search over a list

Pat
While I can use a for loop looking for a match on a list, I was
wondering if there was a one-liner way.

In particular, one of my RE's looks like this '^somestring$' so I can't
just do this: re.search( '^somestring$', str( mylist ) )

I'm not smart enough (total newbie) to code up a generator expression
and I was wondering if I'm missing something obvious.

I love succinct but clearly understandable code.

thx!
Oct 19 '08 #1
5 1071
Pat
Bruno Desthuilliers wrote:
Pat a écrit :
>While I can use a for loop looking for a match on a list, I was
wondering if there was a one-liner way.

In particular, one of my RE's looks like this '^somestring$' so I
can't just do this: re.search( '^somestring$', str( mylist ) )

I'm not smart enough (total newbie) to code up a generator expression
and I was wondering if I'm missing something obvious.

words = ['foo', 'bar', 'somestring', 'baaz']
re.search(r"^somestring$", "\n".join(words), re.MULTILINE)

>I love succinct but clearly understandable code.

separator.join(sequence_of_strings) is a very common python idiom, so
you can consider it as readable.
That's excellent. Exactly what I was looking for.

Thank you VERY much!
Oct 20 '08 #2
Pat a écrit :
Bruno Desthuilliers wrote:
>Pat a écrit :
>>While I can use a for loop looking for a match on a list, I was
wondering if there was a one-liner way.

In particular, one of my RE's looks like this '^somestring$' so I
can't just do this: re.search( '^somestring$', str( mylist ) )

I'm not smart enough (total newbie) to code up a generator expression
and I was wondering if I'm missing something obvious.

words = ['foo', 'bar', 'somestring', 'baaz']
re.search(r"^somestring$", "\n".join(words), re.MULTILINE)

>>I love succinct but clearly understandable code.

separator.join(sequence_of_strings) is a very common python idiom, so
you can consider it as readable.

That's excellent. Exactly what I was looking for.

Thank you VERY much!
Note that at least for this exact case, you don't need re at all:
>>'somestring' in words
True

But I guess you do have some less trivial use case !-)
Oct 20 '08 #3
Pat wrote:
Bruno Desthuilliers wrote:
>Pat a écrit :
>>While I can use a for loop looking for a match on a list, I was
wondering if there was a one-liner way.

In particular, one of my RE's looks like this '^somestring$' so I
can't just do this: re.search( '^somestring$', str( mylist ) )

I'm not smart enough (total newbie) to code up a generator expression
and I was wondering if I'm missing something obvious.

words = ['foo', 'bar', 'somestring', 'baaz']
re.search(r"^somestring$", "\n".join(words), re.MULTILINE)

>>I love succinct but clearly understandable code.

separator.join(sequence_of_strings) is a very common python idiom, so
you can consider it as readable.

That's excellent. Exactly what I was looking for.
I suspect that

any(re.match(pat, word) for word in words)

might be a more efficient way to do this.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Oct 23 '08 #4
Steve Holden a écrit :
Pat wrote:
>Bruno Desthuilliers wrote:
(snip)
>>words = ['foo', 'bar', 'somestring', 'baaz']
re.search(r"^somestring$", "\n".join(words), re.MULTILINE)
(snip)
>>
I suspect that

any(re.match(pat, word) for word in words)

might be a more efficient way to do this.
Indeed. I'm not yet used to have all and any builtins, thanks for the
reminder.
Oct 23 '08 #5
Pat
Bruno Desthuilliers wrote:
Pat a écrit :
>Bruno Desthuilliers wrote:
>>Pat a écrit :
While I can use a for loop looking for a match on a list, I was
wondering if there was a one-liner way.

In particular, one of my RE's looks like this '^somestring$' so I
can't just do this: re.search( '^somestring$', str( mylist ) )

I'm not smart enough (total newbie) to code up a generator
expression and I was wondering if I'm missing something obvious.

words = ['foo', 'bar', 'somestring', 'baaz']
re.search(r"^somestring$", "\n".join(words), re.MULTILINE)
I love succinct but clearly understandable code.

separator.join(sequence_of_strings) is a very common python idiom, so
you can consider it as readable.

That's excellent. Exactly what I was looking for.

Thank you VERY much!

Note that at least for this exact case, you don't need re at all:
>>'somestring' in words
True

But I guess you do have some less trivial use case !-)
I used re because I wanted a string that was not a substring, hence the
^$. In my trivial example, I used words but my intent was to search for
words within a longer string.
Oct 23 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by RAJASEKHAR KONDABALA | last post: by
1 post views Thread by cyrvb | last post: by
10 posts views Thread by free2cric | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.