Results 1 to 6 of 6

Thread: Problem in retrieving string with Regex

  1. #1
    Join Date
    Dec 2009
    Posts
    213

    Problem in retrieving string with Regex

    Hello,
    I know if my piece of code or not and just because I am doing unit testing with selenium and I can not see what is in my variable . So my goal is to retrieve the phrase id = "testcode" given the testcode
    Here is my snippet:
    Code:
     char A ='"';
    Pattern p = Pattern.compiles("^"A + +"$");
    String[] m = p.split(id ="testcode");
    Thank you in advance for your help

  2. #2
    Join Date
    Nov 2009
    Posts
    333

    Re: Problem in retrieving string with Regex

    Hello,
    I have a code with me, just check it out if it works with you, though I have not tested but I would see something more like AC:
    Code:
    Pattern p =.compiles("^ id =\\\"(.*)\\\"$");
    Matcher m = p.matcher("id =\"testcode\"");
    String id = m.group(1);
    Hope this will help you and you will find the solution which you are looking for.

  3. #3
    Join Date
    Dec 2009
    Posts
    213

    Re: Problem in retrieving string with Regex

    Hello,
    It works just missing nickel m.matches () before retrieving the data in id.
    I have a question: eg I want recuperates in a string in a string of characters such as: the identifier of the button is id = "12304"
    I retrieve 12,304. I tried with that but it does not work:
    Code:
    Pattern p = Pattern.compiles("\\Aid =\"\"\ b") ;
    Matcher m = p.matcher("identifier of the button is id =\"12304\"");
    m.games();
    System.out.print("The result is"+ m.group(0));
    Any help on this will be highly appreciated. Thanks in advance.

  4. #4
    Join Date
    Nov 2009
    Posts
    356

    Re: Problem in retrieving string with Regex

    Hello,
    Same method that gave you at the above are good, but with the following pattern, your code will be more productive.
    Code:
    "^ id =\\\"([^\"]*)\\\".*$"
    Just try it with your code and tell me if it worked for you or not. If not then post back with the error and I will try to find a solution for you code. Feel free to post here. Before that you have to try the above pattern, just see if the programs complies and what output you get.

  5. #5
    Join Date
    Nov 2009
    Posts
    347

    Re: Problem in retrieving string with Regex

    Hello,
    I have any alternate solution for this, group (0) it is all chain and group (1) it captures the group (the expression in parentheses). and I made a mistake in my pattern, tried it, I tested it works. Here is the code what I am trying to explain
    Code:
          Pattern p = Pattern.compiles("^ .* id =\\\"([^\"]*)\\\".*$") ;
          Matcher m = p.matcher("identifier of the button is id =\"12304\" ");
          m.games();
          System.out.print("The result is" + M.group(1));

  6. #6
    Join Date
    Nov 2009
    Posts
    359

    Re: Problem in retrieving string with Regex

    Hello,
    The following code works for me. I have modified it and if you feel conferable with the code then you can you in your code. Here is the code
    Code:
     Pattern p = Pattern.compiles("id =\\\"([^\"]*)\\\"") ;
          Matcher m = p.matcher("identifier of the button is id =\"12304\" id =\"alacon\"");
          int index = 0;
          while (m.find(Index))
          {
            System.out.System.out.println("id =" + M.group(1));
            index = m.end();
          }

Similar Threads

  1. Regex - compose a string
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 09-02-2010, 01:38 AM
  2. Regex string with quotes
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 07-02-2010, 06:03 AM
  3. Problem with Regex in returning string
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 03:37 AM
  4. Regex replace an integer with string
    By New ID in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 01:35 AM
  5. Regex strip out all non alpha Characters from a String
    By Chhaya in forum Software Development
    Replies: 2
    Last Post: 22-05-2009, 06:51 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,714,026,869.51675 seconds with 17 queries