Results 1 to 4 of 4
  1. #1
    rajesh76 is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default How to find string pattern written between 2 ### in a paragraph

    Hi,
    The project i'm working on is like this..
    i have a String variable which stores something like a paragraph. from this i need to find all the occurances of the substring ###_______###. the value between ### can be anything..

    i was thinking of using the split() function and comparing if each string stored has 2 ### patterns. but this seems a very inefficient way of coding, especially since the number of words can be huge.

    can u please suggest any approach or function that i could use.. i don't know much abt regex, i have tried searching, but haven't really found anything useful yet..

    thanks in advance..

  2. #2
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    I'm not sure what you want but finding the following three substrings is quite easy:
    Java Code:
    import java.util.ArrayList;
    
    public class Paragraph {
        public static void main(String[] args) {
            ArrayList<String> strings = new ArrayList<String>();
            String s1 = "###substring###";
            String s2 = "###example###";
            String s3 = "###whatever###";
            strings.add(s1);
            strings.add(s2);
            strings.add(s3);
            
            for (String s : strings) {
                int indexOfFirstChar = s.indexOf("###")+3;
                int indexOfLastChar = s.indexOf("###", indexOfFirstChar)-1;
                System.out.println(s.substring(indexOfFirstChar, indexOfLastChar+1));
            }
        }
    }
    which returns the output:
    substring
    example
    whatever

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,251
    Rep Power
    19

    Default

    i don't know much abt regex
    Start learning here:
    Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns
    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
    and of course the Pattern API.

    What you want is a regex that reluctantly matches anything, with positive look-behind and look-ahead that match '###', and to use that with Matcher#find() in a loop.

    db

  4. #4
    rajesh76 is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    thank you for the suggestion and the link, the index method might help. Actually the paragraph i mentioned is an xml converted to a string. i'll have something like <Name>###Black###</Name>. Once i find the pattern i need to check if the opening and closing tags are the same.. any suggestion on how i could try that would be a great help..

Similar Threads

  1. using a loop to form a string pattern
    By xquizitpinay in forum New To Java
    Replies: 7
    Last Post: 10-29-2010, 04:06 AM
  2. find index of string in another string
    By Sdannenberg3 in forum New To Java
    Replies: 4
    Last Post: 03-04-2010, 10:14 AM
  3. String matching a pattern
    By vividcooper in forum New To Java
    Replies: 7
    Last Post: 01-07-2010, 10:30 PM
  4. Replies: 2
    Last Post: 11-11-2009, 05:56 AM
  5. confusion in paragraph
    By JavaJunkie in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-19-2009, 03:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •