• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Java Regular Expressions

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. What have you tried so far? What are the errors? Or do you not know where to start?
 
Ramna Reddy
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try the \d digit metacharacter like the code example

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even the following code giving me "Wrong input"


 
Sri Jad
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even the following code giving me "Wrong input"


 
Rancher
Posts: 43011
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For just alpha characters, try "^\\p{L}*$".
 
Sri Jad
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
See where your hand is? Not there. It's next to this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!