re.search over a list

  • Thread starter Bruno Desthuilliers
  • Start date
B

Bruno Desthuilliers

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.
 
P

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!
 
P

Pat

Bruno said:
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!
 
B

Bruno Desthuilliers

Pat a écrit :
Bruno said:
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:
True

But I guess you do have some less trivial use case !-)
 
S

Steve Holden

Pat said:
Bruno said:
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
 
B

Bruno Desthuilliers

Steve Holden a écrit :
Pat said:
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.
 
P

Pat

Bruno said:
Pat a écrit :
Bruno said:
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:
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.
 

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,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top