473,804 Members | 3,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

trying to match a string

Hi,

Hi,

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.

regards,
SZ

The string may or may not have all the three chars.
Jul 18 '08 #1
20 1938
On Jul 18, 8:33 pm, arnimavidyar... @gmail.com wrote:
Hi,

Hi,

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.

regards,
SZ

The string may or may not have all the three chars.
re.match(r'[LMR]+\Z', your_string)

in English: one or more of L, M , or R, followed by the end of the
string.
Jul 18 '08 #2
oj
On Jul 18, 11:33*am, arnimavidyar... @gmail.com wrote:
Hi,

Hi,

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.

regards,
SZ

The string may or may not have all the three chars.
With regular expressions, [^LRM] matches a character that isn't L, R
or M. So:

import re

var = "LRLRLRLNR"

if re.search(r'[^LRM]', var):
print "Invalid"
Jul 18 '08 #3
On Jul 18, 9:05 pm, oj <ojee...@gmail. comwrote:
On Jul 18, 11:33 am, arnimavidyar... @gmail.com wrote:
Hi,
Hi,
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.
regards,
SZ
The string may or may not have all the three chars.

With regular expressions, [^LRM] matches a character that isn't L, R
or M. So:

import re

var = "LRLRLRLNR"

if re.search(r'[^LRM]', var):
print "Invalid"
Fails if var refers to the empty string.
Jul 18 '08 #4
oj
On Jul 18, 12:10*pm, John Machin <sjmac...@lexic on.netwrote:
On Jul 18, 9:05 pm, oj <ojee...@gmail. comwrote:
On Jul 18, 11:33 am, arnimavidyar... @gmail.com wrote:
Hi,
Hi,
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..
regards,
SZ
The string may or may not have all the three chars.
With regular expressions, [^LRM] matches a character that isn't L, R
or M. So:
import re
var = "LRLRLRLNR"
if re.search(r'[^LRM]', var):
* * print "Invalid"

Fails if var refers to the empty string.
No it doesn't, it succeeds if var is an empty string. An empty string
doesn't contain characters that are not L, R or M.

The OP doesn't specify whether an empty string is valid or not. My
interpretation was that an empty string would be valid.
Jul 18 '08 #5
oj wrote:
On Jul 18, 12:10 pm, John Machin <sjmac...@lexic on.netwrote:
>On Jul 18, 9:05 pm, oj <ojee...@gmail. comwrote:

>>On Jul 18, 11:33 am, arnimavidyar... @gmail.com wrote:

Hi,

Hi,

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.

regards,
SZ

The string may or may not have all the three chars.

With regular expressions, [^LRM] matches a character that isn't L, R
or M. So:

import re

var = "LRLRLRLNR"

if re.search(r'[^LRM]', var):
print "Invalid"
Fails if var refers to the empty string.

No it doesn't, it succeeds if var is an empty string. An empty string
doesn't contain characters that are not L, R or M.

The OP doesn't specify whether an empty string is valid or not. My
interpretation was that an empty string would be valid.
Why not just use * instead of + like:

if re.search(r'^[^LRM]*$', var): # note: ^ outside [] is start of string; $ means end of string
print "Invalid"

This will *only* print invalid when there is a character other than L, R, or M or a empty string.

Jul 18 '08 #6
Andrew Freeman wrote:
oj wrote:
>On Jul 18, 12:10 pm, John Machin <sjmac...@lexic on.netwrote:
>>On Jul 18, 9:05 pm, oj <ojee...@gmail. comwrote:


On Jul 18, 11:33 am, arnimavidyar... @gmail.com wrote:

Hi,
Hi,
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.
regards,
SZ
The string may or may not have all the three chars.
>
With regular expressions, [^LRM] matches a character that isn't L, R
or M. So:
import re
var = "LRLRLRLNR"
if re.search(r'[^LRM]', var):
print "Invalid"

Fails if var refers to the empty string.

No it doesn't, it succeeds if var is an empty string. An empty string
doesn't contain characters that are not L, R or M.

The OP doesn't specify whether an empty string is valid or not. My
interpretati on was that an empty string would be valid.
Why not just use * instead of + like:

if re.search(r'^[^LRM]*$', var): # note: ^ outside [] is start of
string; $ means end of string
print "Invalid"

This will *only* print invalid when there is a character other than L,
R, or M or a empty string.
Sorry, forget the beginning and ending markers, I just tried it out, it
doesn't work.
use this instead:
if re.search(r'[^LRM]*', var):
print "Invalid"
Jul 18 '08 #7
On Jul 18, 7:51*pm, Andrew Freeman <alif...@gmail. comwrote:
Andrew Freeman wrote:
oj wrote:
On Jul 18, 12:10 pm, John Machin <sjmac...@lexic on.netwrote:
>On Jul 18, 9:05 pm, oj <ojee...@gmail. comwrote:
>>On Jul 18, 11:33 am, arnimavidyar... @gmail.com wrote:
>>>Hi,
* * * * Hi,
* * * * 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.
* * * * regards,
SZ
* * * * The string may or may not have all the three chars.
>>With regular expressions, [^LRM] matches a character that isn't L, R
or M. So:
* * * import re
* * * var = "LRLRLRLNR"
* * * if re.search(r'[^LRM]', var):
* * print "Invalid"
>Fails if var refers to the empty string.
No it doesn't, it succeeds if var is an empty string. An empty string
doesn't contain characters that are not L, R or M.
The OP doesn't specify whether an empty string is valid or not. My
interpretation was that an empty string would be valid.
Why not just use * instead of + like:
if re.search(r'^[^LRM]*$', var): # note: ^ outside [] is start of
string; $ means end of string
* *print "Invalid"
This will *only* print invalid when there is a character other than L,
R, or M or a empty string.

Sorry, forget the beginning and ending markers, I just tried it out, it
doesn't work.
use this instead:

if re.search(r'[^LRM]*', var):
* *print "Invalid"
I was using kodos to check the regex.I should have used the IDE
instead.Thanks a lot again.
Jul 18 '08 #8
On Jul 18, 7:51 am, Andrew Freeman <alif...@gmail. comwrote:
Andrew Freeman wrote:
oj wrote:
On Jul 18, 12:10 pm, John Machin <sjmac...@lexic on.netwrote:
>On Jul 18, 9:05 pm, oj <ojee...@gmail. comwrote:
>>On Jul 18, 11:33 am, arnimavidyar... @gmail.com wrote:
>>>Hi,
Hi,
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.
regards,
SZ
The string may or may not have all the three chars.
>>With regular expressions, [^LRM] matches a character that isn't L, R
or M. So:
import re
var = "LRLRLRLNR"
if re.search(r'[^LRM]', var):
print "Invalid"
>Fails if var refers to the empty string.
No it doesn't, it succeeds if var is an empty string. An empty string
doesn't contain characters that are not L, R or M.
The OP doesn't specify whether an empty string is valid or not. My
interpretation was that an empty string would be valid.
Why not just use * instead of + like:
if re.search(r'^[^LRM]*$', var): # note: ^ outside [] is start of
string; $ means end of string
print "Invalid"
This will *only* print invalid when there is a character other than L,
R, or M or a empty string.

Sorry, forget the beginning and ending markers, I just tried it out, it
doesn't work.
use this instead:

if re.search(r'[^LRM]*', var):
print "Invalid"
This won't work -- every string in the universe contains 0 or more
characters which are not 'L', 'R', or 'M'. That is, the regular
expression X* could match the empty string, which can be found in all
strings.
Jul 18 '08 #9
oj
>
Why not just use * instead of + like:
if re.search(r'^[^LRM]*$', var): # note: ^ outside [] is start of
string; $ means end of string
* *print "Invalid"
This will *only* print invalid when there is a character other than L,
R, or M or a empty string.

Sorry, forget the beginning and ending markers, I just tried it out, it
doesn't work.
use this instead:

if re.search(r'[^LRM]*', var):
* *print "Invalid"
No, that's broken.

That searches for any number of invalid characters. Even 0, so it
ALWAYS matches, no matter what string you give it.

My regex worked in the first place, you're complicating it needlessly.
The presence of one invalid character makes the string invalid, so why
not just search for one? Similarly, there's no need to stick in the
beginning and end markers - you're not trying to match the entire
string, just find part of it that is invalid.

However, I think the sets solution by Scott David Daniels is the most
elegant method put forward.
Jul 18 '08 #10

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

Similar topics

9
10360
by: Ron Adam | last post by:
Is it possible to match a string to regular expression pattern instead of the other way around? For example, instead of finding a match within a string, I want to find out, (pass or fail), if a string is a partial match to an re. Given an re of 'abcd and a bunch of other stuff' This is what i'm looking for:
3
2534
by: bdwise | last post by:
I have this in my body tag: something();something(); document.thisForm.textBox1.focus();something(); And I want to find a part between the semicolons that ends in focus() and remove the entire value between the semicolons. My Regular Expression looks like this but it is not matching, can anyone help?
1
3370
by: gohaku | last post by:
Hi everyone, I am having a problem with the match function: import re string = 'abc 123456 xyz' if re.match("\d{1,}",string): print "Found a number" #Does not print whereas findall works:
1
3983
by: Venkat | last post by:
Hi, I am using match function of string to find if a character is there in a string. The function Match is working fine with all the other characters except when the searching character is "+". Here is the piece of code i am using var line1 = "Hell+O";
3
5326
by: Jeff McPhail | last post by:
I am using Regex.Match in a large application and the memory is growing out of control. I have tried several ways to try and release the memory and none of them work. Here are some similar examples of what I have tried... string testString = "lkf slkdjflksd sdfjlksdjff fsdjlsdfj flk;sjkf"; while(true) { Regex .Match(testString,@"(\w)"); } ---------------------------------------------------------------------- string testString = "lkf...
2
1541
by: RAW | last post by:
Hi Im trying to us relative path in my connection string instead of absolute what Im doing wrong this is my code Sub Page_Load() Dim strConnection as String = "Provider=Microsoft.Jet.OLEDB.4.0;" strConnection += "Data Source=Server.MapPath("../dbcontainer/content.mdb") data_src.text = strConnection
4
2299
by: 28tommy | last post by:
Hi, I'm trying to find scripts in html source of a page retrieved from the web. I'm trying to use the following rule: match = re.compile('<script + src=+>') I'm testing it on a page that includes the following source: <script language="JavaScript1.2"
6
3364
by: AppleBag | last post by:
I'm having the worst time trying to login to myspace through code. Can someone tell me how to do this? Please try it yourself before replying, only because I have asked this a couple of times in the past in other places, and while the help was much appreciated, it seemed everyone just wanted to 'theoretically' explain how to do it, but when I tried to do it myself, I couldn't login. I want to simply pass the email address and password to...
4
7671
by: Dylan Nicholson | last post by:
I can write a regular expression that will only match strings that are NOT the word apple: ^(.*|a.*|ap.*|app.*|apple.+)$ But is there a neater way, and how would I do it to match strings that are NOT the word apple OR banana? Then what would be needed to match only strings that do not CONTAIN the word "apple" or "banana" or "cherry"?
0
9588
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10589
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10085
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7625
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2999
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.