aspose file tools
The moose likes Java in General and the fly likes Java Regular Expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Java Regular Expressions" Watch "Java Regular Expressions" New topic
Author

Java Regular Expressions

Ramna Reddy
Ranch Hand

Joined: Aug 06, 2006
Posts: 96
Hi ranchers,
Iam new working with java regular expressions,I need reg-ex for accepting only alpha characters(A-Z a-z),and anything else should be thrown as error.

Any help,
Thanks in advance.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Ok. What have you tried so far? What are the errors? Or do you not know where to start?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ramna Reddy
Ranch Hand

Joined: Aug 06, 2006
Posts: 96
Thank you Paul,for your quick reply ,

This is my code..

String Charmatch = "^[a-zA-Z]"; //dont know whether this is correct.
if( !(lname.matches(Charmatch))){
-----
}//

The requirement is, a text box which should allow only characters,anything else should throw error.
what I need is ,a regular expression which matches the requirement.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Did you try the \d digit metacharacter like the code example



Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Sri Jad
Ranch Hand

Joined: Apr 25, 2005
Posts: 49
Hi Remko and other ranchers,

I have the same requirement and i tried the following code , but still Its givig me "Wrong input" , any suggestion is very much appreciable.Thanks In Advance.

Sri Jad
Ranch Hand

Joined: Apr 25, 2005
Posts: 49
Even the following code giving me "Wrong input"


Sri Jad
Ranch Hand

Joined: Apr 25, 2005
Posts: 49
Even the following code giving me "Wrong input"


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35254
    
    7
For just alpha characters, try "^\\p{L}*$".


Android appsImageJ pluginsJava web charts
Sri Jad
Ranch Hand

Joined: Apr 25, 2005
Posts: 49
This code works and gives following results

true:
false:
false:
false:




private void isNumber(String value) {
if (((Pattern.compile("(\\d+?)").matcher(value).matches()))) {
System.out.println("true:");
} else {
System.out.println("false:");
}
}
}
Sri Jad
Ranch Hand

Joined: Apr 25, 2005
Posts: 49
import java.util.regex.Pattern;
public class UtilsTest {
public static void main(String[] args) {
UtilsTest ut = new UtilsTest();
ut.isNumber("123456");
ut.isNumber("12test");
ut.isNumber("testit");
ut.isNumber("test12");
}
private void isNumber(String value) {
if (((Pattern.compile("(\\d+?)").matcher(value).matches()))) {
System.out.println("true:");
} else {
System.out.println("false:");
}
}
}
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
I dont't exactly know what your problem is, but your code which doesn't compile because it should be public static void main (Sting... args).

Isn't giving the feedback "Wrong input" by me.

The pattern \d looks for a digit which is certainy not in the String
"hjhjkhkj".
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Java Regular Expressions
 
Similar Threads
regex and backslash..
strings and regular expressions.
SWIFT Message Format Check Using Java
general question
Regular Expression Question