473,387 Members | 1,621 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,387 software developers and data experts.

regular expression and case specificality

Can anyone tell me how to write a regular expression that will match strings that either has only lower case or only upper case characters in it. For example, it will match AM, PM, am or parrot but not match Am, aM, Pm or Parrot.
Dec 9 '12 #1
5 2125
Rabbit
12,516 Expert Mod 8TB
The | character will let you logically OR two conditions. Create one for just lower case characters and one for just upper case characters. Don't forget to set the ignore case to off.
Dec 9 '12 #2
Hi Rabbit, thanks for the answer.

I have actually been trying to use the OR before itself, but then I understood that I am doing wrong when I look for lower case characters itself.

I thought the expression : em=re.compile('[a-z]')
will give the correct match for string with lower case only characters, but it is not. It gave the string match for strings like 'asEer'.

Can you please tell me the way to make the regex search for characters throughout the string for lower case characters?
Dec 9 '12 #3
I can actually do this in the following expression:

re.match('[a-z]{2}|[A-Z]{2}','ab')

in which the {2} gives the length of the string to be matched. But is there anyway I can make the length of the string to be a variable, like

c=len(str)
re.match('[a-z]{c}|[A-Z]{c}','ab')

where variable 'c' is the length of the string. This doesnt work and 're' is not able to take a variable name inside the {}
Dec 9 '12 #4
Rabbit
12,516 Expert Mod 8TB
There's no need to specify a length. You can use a * to match 0-infinite characters or a + to match 1-infinite characters. Then you just need to couple it with the ^ and $ characters. Where ^ demarcates the beginning of the string and $ demarcates the end of the string.
Dec 9 '12 #5
thanks man. i added all ^,$,+ as u said. it worked.
Dec 9 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Bryce Budd | last post by:
Hi all, I am trying to use a regular expression validator to check for the existence of PO Box in an address textbox. The business rule is "No addresses with PO Boxes are allowed." What I...
8
by: Rajeev Soni | last post by:
Hi I am looking for the regular expression for validating the allowed file types to upload like files like "zip,pdf,doc,rtf,gif,jpg,png,txt"; and the expression should not be case sensitive like...
7
by: Chris Kennedy | last post by:
Does anyone know a regular expression that will validate the file extension but also allow multiple file extensions if necessary. It also needs to be case insensitive. Basically, what I want is to...
2
by: Brian Kitt | last post by:
I have a process where I do some minimal reformating on a TAB delimited document to prepare for DTS load. This process has been running fine, but I recently made a change. I have a Full Text...
1
by: Stan | last post by:
I have a very simple web form with a textbox and a regular expression validator. The regular expression I'm attempting to use doesn't work on the client side (Javascript) tho it works on the server...
15
by: Mark Rae | last post by:
Hi, I'm trying to construct a RegEx pattern which will validate a string so that it can contain: only the numerical characters from 0 to 9 i.e. no decimal points, negative signs, exponentials...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
5
by: Grzegorz Danowski | last post by:
Hi, I'd like to read all lines of caption text from a string: .... Name ="Paragraph" Caption ="The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. " "The...
3
by: gast128 | last post by:
Dear all, I was looking for a c++ implementation of regular expressions using the Interpreter Design Pattern. GOF only references a Smaltalk implementation which I can not read :(. Does...
18
by: Lit | last post by:
Hi, I am looking for a Regular expression for a password for my RegExp ValidationControl Requirements are, At least 8 characters long. At least one digit At least one upper case character
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.