Results 1 to 5 of 5
  1. #1
    samnjugu is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Find character at the end of the string with regex

    I have a simple regex that I cant get to work, it is supposed to check that the last character in a string is an integer.

    String myString = "dst760.log.092211.233116";
    if(myString.matches("\\d$")
    {
    do something
    }
    else
    {
    do something else
    }

    The format of the string changes but the only thing I want to check is the last character. I have tried a few iterations without any success here are a few of them ("[0-9]$")("^*.[\\d+]$") and few more that I cant remember.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Find character at the end of the string with regex

    You shouldn´t use matches( ... ) because it tries to match the entire String against the regular expression. Use find() and read the API documentation for the Matcher class.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    samnjugu is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: Find character at the end of the string with regex

    Thanks for pointing me in the right direction, I will try find().

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Find character at the end of the string with regex

    Quote Originally Posted by samnjugu View Post
    Thanks for pointing me in the right direction, I will try find().
    Of course you can also do without those nasty regular expressions; have a look:

    Java Code:
    boolean isLastDigit(String s) {
       if (s == null || s.length() == 0) return false; // no last digit in here
       char l= s.charAt(s.length()-1); // the last char
       return Character.isDigit(c);
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    samnjugu is offline Member
    Join Date
    Sep 2011
    Posts
    4
    Rep Power
    0

    Default Re: Find character at the end of the string with regex

    Hi Jos, I have been using a method almost exactly as yours, after having no success with the regex as for some reason my matcher class does not have a find() method. In my version since I check for null before calling this method I have just shrunk it to a oneliner.

    Java Code:
    static boolean lastCharIsDigit(String string)
        {    	    	
    		return Character.isDigit(string.charAt(string.length() - 1));
        	
        }
    Thanks for the help.

Similar Threads

  1. String operations or regex please
    By africanhacker in forum New To Java
    Replies: 7
    Last Post: 03-31-2011, 08:14 PM
  2. parse simple string with regex?
    By zardos in forum New To Java
    Replies: 1
    Last Post: 03-01-2011, 01:14 PM
  3. how do I find acsii character?
    By pinar in forum New To Java
    Replies: 7
    Last Post: 10-29-2010, 07:46 AM
  4. Regex: find all possible matches
    By Alessio in forum New To Java
    Replies: 3
    Last Post: 10-19-2010, 02:10 PM
  5. REGEX - predefined character classes
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-10-2008, 11:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •