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 3 of 3

Thread: how to extract specific part of a string in java

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to extract specific part of a string in java

    consider this statement from a jsp file(there are many more statements like this in jsp file..) Statement-

    <h:dataGrid something styleclass="styleclass1" something1
    onClick="event" something2
    <% this is a scriplet tag %>
    something3
    style="style1">

    <h:output text>hello i am text</h:output text>

    </h:dataGrid>
    what i want is to extract(and store it somewhere) the part from "<" to ">" where:

    < - is the one in "<h:dataGrid"
    > - is the one in "style1>" and not the('>') one that appears in the end
    of "</h:dataGrid>" or "<h:output text>" or "</h:output text>"
    problem is the text b/w && is in multi-line...&& there are scriplet tags in between them.. so i don't know how to extract this particular string.. i tried using using some regular expressions but couldn't find the exact one..

    (this was just an example && instead of this "" tag it can be anything like again in this line :

    <h:output text>hello i am text</h:output text>
    i want to extract the string from "<" till ">" where :

    < - is the one in starting of "<h:output text>"
    > - is the one in ending of "<h:output text>" and not the one in "</h:output text>"
    however the difference b/w this exmple and the above mentioned one is that this one is not multi-line and doesn't contains any scriplet tags )

    Can someone please help me out on this..any specific approach or regex??


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,362
    My Mood
    Mellow
    Thanks
    254
    Thanked 786 Times in 771 Posts

    Default Re: how to extract specific part of a string in java

    Welcome! Please read this topic to learn how to post code correctly and other useful info for new members.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: how to extract specific part of a string in java

    Please spend a few minutes to go through the topic posted by GregBrannon above. Your original post is hard to read and understand.

    It seems that you're looking for a way to extract JSP tags where the tags:
    • can be on a single line or multiple lines
    • may contain scriptlets

    If your JSP files are written in XML format, then you can use an XML parser to do the job. However from your example this does not seem to be the case.

    You can try using Jasper to get an object model from your JSP page. See Hacking Jasper to Get Object Model of a JSP Page | Java Code Geeks.

    You can certainly use regex to do this, but be aware that it's an iterative process as regex is not easy to learn, and a single character mistake can cause the regex parser to fail for some or all cases.

    If you want to go with the regex approach, the following should help you as a starting point:

    Pattern tagPattern = Pattern.compile(".*?(<.+?[^%]>).*", Pattern.DOTALL);

    It should work for the 2 cases in your post (based on my understanding, which may be wrong, hence the call for you to properly format and post your code). (Note the use of a capturing group in the regex.)

    If you need to ask further questions on this, please post it in the following format:

    Original string:
    Paste your original string here

    Extracted string:
    Write the bit to be extracted from the original string here

    You'd also want to write unit tests to test and regression-test if the above expression does not work for other cases that you may have in your JSP files. This is so that as you modify the regex, you don't accidentally cause it to fail for earlier cases that worked.

    Further info on regex:

Similar Threads

  1. extract specific text from a string
    By smemamian in forum Android Development
    Replies: 2
    Last Post: January 18th, 2014, 05:42 AM
  2. Editing a Text File in Java, a specific part.
    By sakonpure6 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2013, 03:32 PM
  3. Replies: 7
    Last Post: May 28th, 2013, 10:03 AM
  4. Replies: 3
    Last Post: October 26th, 2012, 03:19 PM
  5. How to write to specific part of an html file using java
    By nasi in forum File I/O & Other I/O Streams
    Replies: 12
    Last Post: May 28th, 2010, 12:22 AM

Tags for this Thread