Results 1 to 5 of 5
  1. #1
    ShaneO is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Question [Soved] lThe Contains method - or regex - is not working

    I'm trying to search a string for the "@" character but it seems as though my regex is not working..

    Here is what I have:

    Java Code:
    CharSequence cs = ".*@.*";
    CharSequence cs2 = ".*@a-b.c.d";
    
    boolean csRet = users[i].contains(cs); //Currently everything returns false.
    				
    	if (!csRet) {
    		users[i] += "@a-b.c.d";
    	}
    
    boolean cs2Ret = users[i].contains(cs2);
    				
    	if (cs2Ret) {
                   //Do stuff
            }
    Last edited by ShaneO; 10-18-2013 at 05:54 PM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,813
    Rep Power
    5

    Default Re: The Contains method - or regex - is not working (Trying to look for the @ symbol)

    What is user? The String contains method does not expect a regex.

    Regards,
    Jim
    Last edited by jim829; 10-18-2013 at 05:40 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    ShaneO is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: The Contains method - or regex - is not working (Trying to look for the @ symbol)

    Quote Originally Posted by jim829 View Post
    What is user? The String contains method does not expect a regex.

    Regards,
    Jim
    The 'user' is a string containing an email address.

    Is there another way I can check for the "@" symbol without using regex? It seems to be a pain, especially for someone like me who doesn't know regex well :S

    For the second part I'm also looking for a longer string, but that also fails.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,813
    Rep Power
    5

    Default Re: The Contains method - or regex - is not working (Trying to look for the @ symbol)

    You can still use contains. It just doesn't support regex characters -- (it treats them normally). So just check for a @ and nothing else. If you want to see where it is in the string you can use indexOf. If indexOf returns -1, it doesn't exist. Otherwise it returns its 0-based position.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    ShaneO is offline Member
    Join Date
    May 2012
    Posts
    7
    Rep Power
    0

    Default Re: The Contains method - or regex - is not working (Trying to look for the @ symbol)

    Quote Originally Posted by jim829 View Post
    You can still use contains. It just doesn't support regex characters -- (it treats them normally). So just check for a @ and nothing else. If you want to see where it is in the string you can use indexOf. If indexOf returns -1, it doesn't exist. Otherwise it returns its 0-based position.

    Regards,
    Jim
    That worked! Thanks a lot! I should try looking for simple ways of doing things next time before diving into something like regex :S

Similar Threads

  1. Cannt find symbol - how do I fix this/why isn't it working?
    By cheesegraterish in forum New To Java
    Replies: 4
    Last Post: 06-06-2013, 10:20 PM
  2. Replies: 7
    Last Post: 04-28-2012, 03:10 AM
  3. Replies: 2
    Last Post: 03-08-2011, 04:08 PM
  4. HELP! can't find symbol-method
    By Jack9333 in forum New To Java
    Replies: 2
    Last Post: 03-04-2011, 02:48 AM
  5. cannot find symbol - method iterator()
    By D.Calladine in forum New To Java
    Replies: 1
    Last Post: 11-30-2010, 01:13 PM

Posting Permissions

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