Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Pawan Choure
Greenhorn
Posts: 27
Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..


 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pawan Choure
Greenhorn
Posts: 27
Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then why not swap it around? Check for "^\\d+$" - only digits.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or try parsing it with Integer.parseInt()
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But 111111AAA1 has more than 8 digits in, and parsed as a hexadecimal will throw a NumberFormatException.
 
Pawan Choure
Greenhorn
Posts: 27
Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob:I know i can accomplish this with but i wanted to know why i am not able to use
 
Pawan Choure
Greenhorn
Posts: 27
Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
Eclipse IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawan Choure wrote: . . . i need regex for the following mentioned scenario. . . .
You have already been told what you can use.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!