By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,386 Members | 1,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,386 IT Pros & Developers. It's quick & easy.

Re: regex doubts

P: n/a
Mr SZ wrote:
I am taking a string as an input from the user and it should only
contain the chars:L , M or R

I tried the folllowing in kodos but they are still not perfect:

[^A-K,^N-Q,^S-Z,^0-9]
[L][M][R]
[LRM]?L?[LRM]? etc but they do not exactly meet what I need.

For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N' .like that.
try "[LRM]+$" (an L or an R or an M, one or more times, all the way to
the end of the string).

</F>

Jul 19 '08 #1
Share this Question
Share on Google+
8 Replies

P: n/a
On Jul 20, 5:04 am, Fredrik Lundh <fred...@pythonware.comwrote:
Mr SZ wrote:
I am taking a string as an input from the user and it should only
contain the chars:L , M or R
I tried the folllowing in kodos but they are still not perfect:
[^A-K,^N-Q,^S-Z,^0-9]
[L][M][R]
[LRM]?L?[LRM]? etc but they do not exactly meet what I need.
For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N' .like that.

try "[LRM]+$" (an L or an R or an M, one or more times, all the way to
the end of the string).
Ummm ... with the default flag settings, shouldn't that be \Z instead
of $
?

Jul 19 '08 #2

P: n/a
On Jul 19, 9:12*pm, John Machin <sjmac...@lexicon.netwrote:
On Jul 20, 5:04 am, Fredrik Lundh <fred...@pythonware.comwrote:
Mr SZ wrote:
I am taking a string as an input from the user and it should only
contain the chars:L , M or R
I tried the folllowing in kodos but they are still not perfect:
[^A-K,^N-Q,^S-Z,^0-9]
[L][M][R]
[LRM]?L?[LRM]? etc but they do not exactly meet what I need.
For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N' .like that..
try "[LRM]+$" (an L or an R or an M, one or more times, all the way to
the end of the string).

Ummm ... with the default flag settings, shouldn't that be \Z instead
of $
?
$ means end of string unless the multiline flag is used, in which case
it means end of line.

\Z always means end of string.

Similar remarks apply to ^ and \A.
Jul 19 '08 #3

P: n/a
On Jul 20, 6:35 am, MRAB <goo...@mrabarnett.plus.comwrote:
On Jul 19, 9:12 pm, John Machin <sjmac...@lexicon.netwrote:
On Jul 20, 5:04 am, Fredrik Lundh <fred...@pythonware.comwrote:
Mr SZ wrote:
I am taking a string as an input from the user and it should only
contain the chars:L , M or R
I tried the folllowing in kodos but they are still not perfect:
[^A-K,^N-Q,^S-Z,^0-9]
[L][M][R]
[LRM]?L?[LRM]? etc but they do not exactly meet what I need.
For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N' .like that.
try "[LRM]+$" (an L or an R or an M, one or more times, all the way to
the end of the string).
Ummm ... with the default flag settings, shouldn't that be \Z instead
of $
?

$ means end of string unless the multiline flag is used, in which case
it means end of line.
What manual are you quoting that from? What version of Python are you
using? Can you demonstrate that the pattern "[LRM]+$" will fail to
match the string "L\n"?
Jul 19 '08 #4

P: n/a
John Machin wrote:
>try "[LRM]+$" (an L or an R or an M, one or more times, all the way to
the end of the string).

Ummm ... with the default flag settings, shouldn't that be \Z instead
of $ ?
Why? The OP was reading input from a user; whether he gets a trailing
newline or not depends on the input method, and $ does the right thing
for all normal input methods.

</F>

Jul 20 '08 #5

P: n/a
On Jul 21, 12:30 am, Fredrik Lundh <fred...@pythonware.comwrote:
John Machin wrote:
try "[LRM]+$" (an L or an R or an M, one or more times, all the way to
the end of the string).
Ummm ... with the default flag settings, shouldn't that be \Z instead
of $ ?

Why? The OP was reading input from a user; whether he gets a trailing
newline or not depends on the input method, and $ does the right thing
for all normal input methods.
The goal as far as I can tell was to produce a pattern that matched
one (maybe zero) or more instances of 'L', 'R', or 'M', and no other
characters.
>>bool(re.match(r'[LRM]+\Z', 'MRL\n'))
False
>>bool(re.match(r'[LRM]+$', 'MRL\n'))
True
>>>
'\n' is an "other character".

Perhaps you could explain what you mean by "$ does the right thing".

Jul 20 '08 #6

P: n/a
On Jul 19, 10:44*pm, John Machin <sjmac...@lexicon.netwrote:
On Jul 20, 6:35 am, MRAB <goo...@mrabarnett.plus.comwrote:
On Jul 19, 9:12 pm, John Machin <sjmac...@lexicon.netwrote:
On Jul 20, 5:04 am, Fredrik Lundh <fred...@pythonware.comwrote:
Mr SZ wrote:
I am taking a string as an input from the user and it should only
contain the chars:L , M or R
I tried the folllowing in kodos but they are still not perfect:
[^A-K,^N-Q,^S-Z,^0-9]
[L][M][R]
[LRM]?L?[LRM]? etc but they do not exactly meet what I need.
For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N' .like that.
try "[LRM]+$" (an L or an R or an M, one or more times, all the wayto
the end of the string).
Ummm ... with the default flag settings, shouldn't that be \Z instead
of $
?
$ means end of string unless the multiline flag is used, in which case
it means end of line.

What manual are you quoting that from? What version of Python are you
using? Can you demonstrate that the pattern "[LRM]+$" will fail to
match the string "L\n"?
I see what you mean: $ does match end of line if the newline is the
last character of the string.
Jul 20 '08 #7

P: n/a
John Machin wrote:
'\n' is an "other character".
so how does a user enter that character?
Perhaps you could explain what you mean by "$ does the right thing".
wtf is wrong with you?

</F>

Jul 21 '08 #8

P: n/a
>Perhaps you could explain what you mean by "$ does the right thing".

wtf is wrong with you?
(I mean, you do know under what circumstances $ matches a newline
character when used without modifiers, right? So why do you keep
behaving like a reddit commenter?)

Jul 21 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.