Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Need some Regex help, please.

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    My Mood
    Dead
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need some Regex help, please.

    I'm trying to figure out the correct way of using Regex to filter a String so that it's in this format exactly:
    string1:string2

    I've looked online through some documentation and I can't seem to get it working correctly. This is what I have so far and have tested:
    ^(\\w+)([:{0}])(\\w+)$
    ^\\w[:]\\w$
    ^\\w{1,}[:]{1}\\w{1,}$
    ^[a-z]{1,}&&[:]&&{1}[a-z]{1,}$

    I'm not sure why it does not work from what I read. However, I don't know much about Regex's and am probably making this more difficult than it is. Any help is appreciated, thank you.

    EDIT: I have the String one working, however I now need to allow the String to have whitespace in it:
    ^[a-z]{1,}[:]{1}[a-z]{1,}$

    e.g.
    string 1: string 1

    EDIT 2: After realizing the user will probably have an unassertive amount of spaces, I have come to the conclusion that this works best:
    ^.*[:].*$

    Thanks.
    Last edited by Josheh; December 15th, 2011 at 08:39 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Need some Regex help, please.

    Often I find it helps to break down the expression and concentrate on subsets of the overall goal. In your case, you could concentrate on the first string portion and try to match 'string'. There are different ways to match a word, you could try
    [a-z]+//assuming the engine is set to case insensitive

    Next, how would you add the number to this regex?
    Last edited by copeg; December 15th, 2011 at 08:46 PM.

Similar Threads

  1. help with regex
    By fate0x1 in forum Java SE APIs
    Replies: 1
    Last Post: November 18th, 2011, 03:44 PM
  2. need help with regex
    By o2a1 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 13th, 2011, 07:50 AM
  3. [SOLVED] Java Regex Help
    By newbie in forum Java Theory & Questions
    Replies: 7
    Last Post: January 26th, 2011, 08:01 AM
  4. Need help with Regex
    By snytkine in forum Java Theory & Questions
    Replies: 4
    Last Post: October 12th, 2010, 07:30 AM
  5. Need help with regEx
    By ptabatt in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 14th, 2010, 11:17 AM