Results 1 to 13 of 13
  1. #1
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default regex function, please help

    I know what regex can do but am unable to actually use it, I just do not have the time to learn what I need to implement for an assignment. Can someone please help me.

    I need to parse a string. The string has two formats:

    The first is: "Useless text I want to get rid of then a sign, I want everything that follows and save it as a string, there is no second sign

    Second is: "Useless text I want to get rid of, also get rid of the first and what follows if there is a second . After second take the string after that and save it as a string

    The two possible problems I will face will be all structured this way:

    Java Code:
    "my favourite crisps sell for 2.30"
    Java Code:
    "my favourite vegetable sell @ 1.05/kg so for 5kg I pay 5.25
    In the first example I would want to retain "2.30" in a string. In the second example I would like to retain "5.25" in a string.

    I know a clever programmer could just work with the sign and do something elegant. Takes this as a challenge from a novice :D

  2. #2
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    In either case, you can just split the sentence based on the sign. Here's the code:
    Java Code:
            String s2 = "my favourite vegetable sell @ 1.05/kg so for 5kg I pay 5.25";
            Pattern p = Pattern.compile("");
        
            String[] result = p.split(s2);
            for (int i=0; i < result.length; i++)
                System.out.println(result[i]);

  3. #3
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    I might be mistaken but won't that give you the 1.05 when in fact I want the 5.25?

    The problem i am seeing is that one has to have some logic that recognises when a string has two signs. In that case it ignores the first . In the case where there is one it just takes what is after that.

    Again I might just not be understanding the solution you have offered so please do not be offended.

  4. #4
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    No worries. I was simply showing you how to parse the string based on that special character. Determining if there are 2 signs vs. 1 sign is easy. Just look at the length of the result array. If result.length == 3, then there were 2 signs in the original string. If result.length == 2, then there was just 1 sign in the original string. Get it?

  5. #5
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    I sort of understand but am not very clear on the details. How would I retrieve the values I am interested in?

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Split produces an array of n+1 items, where n is the thing being split upon. If there are two of those symbols you will have 3 items in the array, one for up to the first symbol, one for everything between the two symbols and one for what's after the last symbol.

    Edit: use array indexing to get the strings.

  7. #7
    FlipPoker@gmail.com is offline Senior Member
    Join Date
    Mar 2011
    Posts
    103
    Rep Power
    0

    Default

    The string is split based on the character and stored in the result array. Run the code that I posted. You'll see. The string is split this way:

    result[0] = "my favourite vegetable sell @ ";
    result[1] = "1.05/kg so for 5kg I pay ";
    result[2] = "5.25";

  8. #8
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    Thanks guys. Does that mean I don't get to see some cool regex is action :(

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Probably not, if you can do something easy, you should try to use it. Reg exes can be kind of hard(at least for me they are)

  10. #10
    africanhacker is offline Senior Member
    Join Date
    Feb 2011
    Posts
    107
    Rep Power
    0

    Default

    I'm just being silly guys, thanks for your help

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    No problem, glad to have helped. Please mark your thread solved with the thread tools at the top.

  12. #12
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by africanhacker View Post
    Thanks guys. Does that mean I don't get to see some cool regex is action :(
    Nah, you can also do this with regex.
    Java Code:
            String str = "my favourite vegetable sell @ 1.05/kg so for 5kg I pay 5.25";
            System.out.println ( str.replaceAll(".*" ,"") ) ;
    Note that this is greedy.. it will remove everything from beginning till the last pound sign.

  13. #13
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Quote Originally Posted by FlipPoker@gmail.com View Post
    In either case, you can just split the sentence based on the sign. Here's the code:
    Java Code:
            String s2 = "my favourite vegetable sell @ 1.05/kg so for 5kg I pay 5.25";
            Pattern p = Pattern.compile("");
        
            String[] result = p.split(s2);
            for (int i=0; i < result.length; i++)
                System.out.println(result[i]);
    there's no need to compile the pattern using Pattern. The String split() method already takes in a regex pattern.

Similar Threads

  1. Calling function in Javascript- from other function
    By jdigger in forum New To Java
    Replies: 1
    Last Post: 02-27-2011, 10:00 PM
  2. Regex help?
    By l3ane in forum New To Java
    Replies: 6
    Last Post: 01-18-2011, 07:04 AM
  3. Help with regex
    By SteroidalPsycho in forum New To Java
    Replies: 2
    Last Post: 03-29-2010, 01:40 AM
  4. Replies: 3
    Last Post: 12-04-2009, 04:08 PM
  5. How "Pattern.matches(regex, input)" function works?
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 04-26-2009, 01:46 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
  •