473,408 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Re: regex doubts

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
8 878
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
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
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
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
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
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
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
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: abbas reji | last post by:
--0-599929911-1059996886=:4358 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Content-Id: Content-Disposition: inline ...
7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
6
by: ritesh | last post by:
Hi, I have been reading some text on C and C++ (i.e advanced books). One of the books mentioned that C++ requires a runtime support whereas C does not - what the author was trying to say was...
9
by: Tim Conner | last post by:
Is there a way to write a faster function ? public static bool IsNumber( char Value ) { if (Regex.IsMatch( Value.ToString(), @"^+$" )) { return true; } else return false; }
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
6
by: Extremest | last post by:
I have a huge regex setup going on. If I don't do each one by itself instead of all in one it won't work for. Also would like to know if there is a faster way tried to use string.replace with all...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
3
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.