• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Regular Expressions

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even the following code giving me "Wrong input"


 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For just alpha characters, try "^\\p{L}*$".
 
Sri Jad
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Honk if you love justice! And honk twice for tiny ads!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic