Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 12-02-2010
Member
 
Join Date: Dec 2009
Posts: 211
Question on regular expressions

Hello,
I try to use a regular expression to check a name entered in a JTextArea. I would allow only the letters of the alphabet (uppercase and lowercase, with or without accents), numbers and a few more characters (space "" dashes "-" and "_" and dot "."). The problem is that I will not understand much about regular expressions. Here is my code
Code:
try
{
	if(! name.equals(""))
	{
		Matcher mt;
Pattern pat = Pattern.compiles("[0-9A-Za-z_ (1)]);
= pat mt.mt(name);
System.out.System.out.println(mt.group());
		if(mt.GroupCount() != 1)
			ok = false;
	}
	else
		ok = false;
}
catch(Exception e)
{
	System.out.System.out.println(e);
}
Reply With Quote
  #2  
Old 12-02-2010
Member
 
Join Date: Nov 2009
Posts: 359
Re: Question on regular expressions

Hello,
The regular expression that you should use is:
Code:
[0-9a-zA-Z _. --]
To test match, you can simply use:
Code:
String s = "test" ;
Boolean ok = string.games("[0-9a-zA-Z _.-]") ;
If you need more information on this you can visit the official site of sun , there you can find the api on java.
Reply With Quote
  #3  
Old 12-02-2010
Member
 
Join Date: Dec 2009
Posts: 211
Re: Question on regular expressions

Hello,
Thank you for the answer, but it does not work completely, unfortunately. In fact it works for a string of one character, and there it is exactly what I want, except for the accents who do not.
So there are two possibilities:
- Either workaround and looping over the length of the word test, and do the same test as there are character (solution quite reasonable given that my application is a personal work)
- Whether there is an expression more complicated words, which manages more than one letter
If you have answers for this then please let me know.
Reply With Quote
  #4  
Old 12-02-2010
Member
 
Join Date: Nov 2009
Posts: 583
Re: Question on regular expressions

Hello,
You can use the following expression for a number of characters
Code:
^[0-9\ \ p{L} _.-]*$
Or you can use this
Code:
^[0-9\ \ p{L} _.-]+$
to have at least one character
Code:
^[0-9\ \ p{L} _.-]{n, m}$
Hope you will use the above in the code and try to run the program.
Reply With Quote
  #5  
Old 12-02-2010
Member
 
Join Date: Nov 2009
Posts: 356
Re: Question on regular expressions

Hello,
Just have a look at the following code
Code:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
Public class Test {
	Public static void hand(String args[]) {
		try {
			String str ="testing";
			boolean k =false;
			if (! str.equals("")) {
				Matcher matcher;
Pattern pat = Pattern.compiles("^[\\p (L) 0-9_ .-]*$");
= pat matcher.matcher(str);
System.out.System.out.println("k ="+ matcher.find());
				/ * System.out.println (matcher.group ());
if (matcher.groupCount ()! = 1)
k = false * /
			} else
				k = false;
		} catch (Exception e) {
			System.out.System.out.println(e);
		}
	}
}
Reply With Quote
  #6  
Old 12-02-2010
Member
 
Join Date: Nov 2009
Posts: 359
Re: Question on regular expressions

Hello,
I am new to this concept in java, but still I have some part of code for you. I do not know that this is what you what, still have a look at it and if it is useful to you then you can use it.
Code:
StringBuffer strbuf = new StringBuffer();
mat = myPattern.matcher("subject");
while (mat.find()) {
  if (checkIfThisMatchShouldBeReplaced()) {
    mat.appendReplacement(strbuf, computeReplacementString());
  }
}
mat.appendTail(strbuf);
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Question on regular expressions"
Thread Thread Starter Forum Replies Last Post
What are an Expressions in PHP? michaels Software Development 5 05-03-2010 05:44 AM
Regular Expressions on the Contents of a File samualres Software Development 4 19-02-2010 10:30 PM
What are the Operators and Expressions in C# ? kyosang Software Development 4 09-02-2010 06:32 AM
Regular Expressions in Java Ash maker Software Development 5 03-02-2010 04:42 AM
Regular expressions for unicode Solaris Software Development 4 15-12-2009 12:20 PM


All times are GMT +5.5. The time now is 10:11 PM.