Results 1 to 7 of 7
  1. #1
    munish is offline Member
    Join Date
    Jul 2009
    Posts
    37
    Rep Power
    0

    Exclamation Extarct Substring using regex.

    Hi,

    i am trying to extract string between [[]] from following string.
    String e = "This is[[my small]]example[[string which I'm going]] to use for pattern matching";
    by using following code :
    Pattern pattern = Pattern.compile("[[(.*?)]]");
    Matcher matcher = pattern.matcher(e);
    while (matcher.find())
    {
    System.out.println(matcher.group());
    }

    }
    Should print result as :
    my small and string which I'm going
    but somehow not getting it could anyone please suggest whts wrong.

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

    Default Re: Extarct Substring using regex.

    First, [] have special meeting to the regex engine. So if you want to match them you have to escape them. So precede each [ or ] with \\.
    Second, group() matches the entire pattern. Use group(1) to match the first capture group.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    munish is offline Member
    Join Date
    Jul 2009
    Posts
    37
    Rep Power
    0

    Default Re: Extarct Substring using regex.

    Thanks jim that helps:)

  4. #4
    munish is offline Member
    Join Date
    Jul 2009
    Posts
    37
    Rep Power
    0

    Default Re: Extarct Substring using regex.

    Hi Jim,

    I am trying to combine 2 regex to print text between them like..
    ==ABC==
    This is Text
    ==DFG==
    this is not.

    I want to print text between ==ABC== and ==DFG==. i am able to identify these by (={2,5}(.*))
    Could you help with this.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Extarct Substring using regex.

    I'm am still not certain what you want to do or how you wanted to do it. Could you provide some more info?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    munish is offline Member
    Join Date
    Jul 2009
    Posts
    37
    Rep Power
    0

    Default Re: Extarct Substring using regex.

    I need to store text between === e.g ABC and DFG which we have already done and text in between ==ABC== and ==DFG== e.g This is Text.

    in second part i am stuck.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Extarct Substring using regex.

    The best that I can offer is that you need to build your regex dynamically and then use that regex to find the string between the two delimiters.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Getting substring ???
    By hacikho in forum New To Java
    Replies: 9
    Last Post: 11-29-2011, 05:17 AM
  2. substring Q
    By arsenal4ever_11 in forum NetBeans
    Replies: 1
    Last Post: 06-01-2010, 03:27 PM
  3. differens between regex and substring
    By aruna1 in forum New To Java
    Replies: 12
    Last Post: 04-11-2009, 05:08 PM
  4. Help with substring
    By hiklior in forum New To Java
    Replies: 12
    Last Post: 06-28-2008, 06:48 AM
  5. How can i get substring
    By 82rathi.angara in forum New To Java
    Replies: 14
    Last Post: 06-27-2008, 04:35 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
  •