File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Regular Expression in JAVA for Checking whether a String contains Alphabet. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Regular Expression in JAVA for Checking whether a String contains Alphabet." Watch "Regular Expression in JAVA for Checking whether a String contains Alphabet." New topic
Author

Regular Expression in JAVA for Checking whether a String contains Alphabet.

Pawan Choure
Greenhorn

Joined: May 11, 2008
Posts: 27

Hi,
I want Regular Expression in JAVA for Checking whether a String contains Alphabet.
I tried

but its not working as expected.It is only checking alphabet in the beginning not in-between

For Example

AAAAAAAAA: Works Fine
1111111AA1: Doesn't Validate

So Anyone know how to get this working then do let me know..



Pawan Choure
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

That's because you use ^ and $. These stand for "start of the string" and "end of the string" respectively. You are literally saying: I want the start of the string, then one single character (are you sure you didn't omit a + or * there?), then the end of the string.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Pawan Choure
Greenhorn

Joined: May 11, 2008
Posts: 27

I tried


Because I wanted to Check whether the Digits user is supposed to enter doesn't contain Alphabet anywhere inside it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Then why not swap it around? Check for "^\\d+$" - only digits.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Or try parsing it with Integer.parseInt()


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
But 111111AAA1 has more than 8 digits in, and parsed as a hexadecimal will throw a NumberFormatException.
Pawan Choure
Greenhorn

Joined: May 11, 2008
Posts: 27

@Rob:I know i can accomplish this with but i wanted to know why i am not able to use
Pawan Choure
Greenhorn

Joined: May 11, 2008
Posts: 27

Campbell Ritchie wrote:But 111111AAA1 has more than 8 digits in, and parsed as a hexadecimal will throw a NumberFormatException.



Can you elaborate more on what you are trying to say. I am not getting what you wanna say.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
You need to write down what those two regular expressions would match, then you will see what they are useful for.

You have been confusing us in this thread: you started saying ". . . whether a String contains Alphabet," and later said you want to check whether the String was a number. Then you said "not alphabet"

Rob is correct; you want to check that the input consists entirely of digits.

111111AAA1 is a valid number in hexadecimal; the comment about 8 digits shows that the parsing method will not help tell whether it is a number.
Pawan Choure
Greenhorn

Joined: May 11, 2008
Posts: 27

Campbell Ritchie wrote:You need to write down what those two regular expressions would match, then you will see what they are useful for.

You have been confusing us in this thread: you started saying ". . . whether a String contains Alphabet," and later said you want to check whether the String was a number. Then you said "not alphabet"

Rob is correct; you want to check that the input consists entirely of digits.

111111AAA1 is a valid number in hexadecimal; the comment about 8 digits shows that the parsing method will not help tell whether it is a number.


See In my Scenario I will accept input from Textbox whose size is 9.User can enter the input and i will validate if it contains Alpabet or not If the given input has alphabet anywhere in the input then validation message should pop up.Basically i can implement that using robert way which i already did but i want to do it by using


ie.
111111111: Valid
AAAAAAAAA:Invalid
1111A1111: Invalid
A11131111: Invalid
11111111A: Invalid


Hope you got what i want....
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
Pawan Choure wrote:i want to do it by using


ie.
111111111: Valid
AAAAAAAAA:Invalid
1111A1111: Invalid
A11131111: Invalid
11111111A: Invalid


Hope you got what i want....


Rob told you in the first reply why that doesn't work.
And you've been told an alternative that will work - why can't you use that ?


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

I said before that ^ and $ mean the start and end of the String. You literally are asking for the start, exactly one letter, then the end. You actually want to search for one letter regardless of where in the String it is. Well, letters, punctuation characters, whitespace, and anything else that isn't a digit. Even if you get your regex to work for letters, I can simply add something like !, ~, ? or even spaces and your regex will accept it all. That's why I suggested to allow valid input instead of blocking invalid input. The valid characters (\d) are much more limited than the invalid ones (everything else).
Pawan Choure
Greenhorn

Joined: May 11, 2008
Posts: 27

Rob Spoor wrote:I said before that ^ and $ mean the start and end of the String. You literally are asking for the start, exactly one letter, then the end. You actually want to search for one letter regardless of where in the String it is. Well, letters, punctuation characters, whitespace, and anything else that isn't a digit. Even if you get your regex to work for letters, I can simply add something like !, ~, ? or even spaces and your regex will accept it all. That's why I suggested to allow valid input instead of blocking invalid input. The valid characters (\d) are much more limited than the invalid ones (everything else).


Ya Rob,
I got your point and has done the same.but i just want to know the expression to check whether the given input has any alphabets or not.You Point toward special character is valid but i know how to handle that the only thing i am not able to work withwhich i want to know why it is not working...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16479
    
    2

I have to say that "Must not contain any letters" is a very strange requirement, quite apart from the apparent difficulty of writing a regex to implement it. That certainly isn't the same as "Must only contain digits", which would be a reasonable requirement and is something you almost said earlier in the thread.

So before we flounder about any more could you clarify what is your exact requirement?
Pawan Choure
Greenhorn

Joined: May 11, 2008
Posts: 27

Paul Clapham wrote:I have to say that "Must not contain any letters" is a very strange requirement, quite apart from the apparent difficulty of writing a regex to implement it. That certainly isn't the same as "Must only contain digits", which would be a reasonable requirement and is something you almost said earlier in the thread.

So before we flounder about any more could you clarify what is your exact requirement?


I want to check whether the given inputted value has any alphabet in it.

111111111:Valid
11111111A:Invalid
A11111111:Invalid
1234F1111:Invalid

Hope you got my point..
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 3786
    
    1

What Paul's getting at is that that's a bit unusual. Would these be valid?
11111111?
111&11111
1_1"1'1(1
1*******1

They don't have letters in. Are you really sure they should be valid? If not, then forget about the letters and define your requirements in terms of what is allowed, not what is forbidden.
Pawan Choure
Greenhorn

Joined: May 11, 2008
Posts: 27

Matthew Brown wrote:What Paul's getting at is that that's a bit unusual. Would these be valid?
11111111?
111&11111
1_1"1'1(1
1*******1

They don't have letters in. Are you really sure they should be valid? If not, then forget about the letters and define your requirements in terms of what is allowed, not what is forbidden.



That is invalid but i have handled that and its working fine.i need regex for the following mentioned scenario.
111111111:Valid
11111111A:Invalid
A11111111:Invalid
1234F1111:Invalid
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
Pawan Choure wrote: . . . i need regex for the following mentioned scenario. . . .
You have already been told what you can use.
 
I agree. Here's the link: jrebel
 
subject: Regular Expression in JAVA for Checking whether a String contains Alphabet.
 
Similar Threads
How to use anonymous classes in java
use of regular expression in java
Regular Expression problem
Regex for allowing alpha characters and wildcard %
Pathname Validation - Please help out!!!