Results 1 to 6 of 6

Thread: Question on regular expressions

  1. #1
    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);
    }

  2. #2
    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.

  3. #3
    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.

  4. #4
    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.

  5. #5
    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);
    		}
    	}
    }

  6. #6
    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);

Similar Threads

  1. What are an Expressions in PHP?
    By michaels in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 05:44 AM
  2. Regular Expressions on the Contents of a File
    By samualres in forum Software Development
    Replies: 4
    Last Post: 19-02-2010, 10:30 PM
  3. What are the Operators and Expressions in C# ?
    By kyosang in forum Software Development
    Replies: 4
    Last Post: 09-02-2010, 06:32 AM
  4. Regular Expressions in Java
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 03-02-2010, 04:42 AM
  5. Regular expressions for unicode
    By Solaris in forum Software Development
    Replies: 4
    Last Post: 15-12-2009, 12:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,009,254.07680 seconds with 16 queries