-1

I would like to know the regex to pull data from a string in Java which will have following start and end tag info.

 String temp = "<!--abc Start-->..data...<!--abc end-->"

So bascially, I want to pull everything between

<!--abc Start-->
<!--abc end-->

and these can be in the string n number of time.

Pattern p = Pattern.compile("");
Macher m = p.matcher(temp);

while(m.find()) ....

so basically I am stuck at the regex

Nomad
  • 1,092
  • 11
  • 29
  • 42
  • 4
    You mean to say that the only regex you tried was `""`? – Bart Kiers Jul 13 '11 at 21:49
  • +1 (offsetting) Willing to give young Nomad the benefit of the doubt, methinks Nomad is strong with the Java but not so much with the Regex. – Bob Kaufman Jul 13 '11 at 21:50
  • 2
    it is hard to tell what format that is, but it looks suspiciously like XML and if it is, you [can't parse a non-Regular language like XML with an XML parser](http://stackoverflow.com/search?page=1&tab=votes&q=parse%20xml%20regex) ... –  Jul 13 '11 at 21:51
  • 2
    `"so basically I am stuck at the regex"`: the place to start is a good regex tutorial, and you can find many via Google. – Hovercraft Full Of Eels Jul 13 '11 at 21:51
  • 1
    This looks like you are trying to parse XML with regexes, which is a REALLY BAD IDEA™. Please read the first answer to http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Jim Garrison Jul 13 '11 at 21:53
  • 2
    @Bob - not so strong with the searching either ... aren't there already 7-zillion questions on SO asking for a regex to extract data between tags? – Stephen P Jul 13 '11 at 21:55
  • 1
    @Stephen - You're absolutely right. My first instinct, as should @Nomad's, would be to hit the search engines, especially when you consider that 99.999% of the problems I've encountered have been encountered, and well documented, by better software engineers who have preceded me. Still, I have a soft spot for newbies here, especially since my first question was downvoted and closed for reasons that mystified me at the time. – Bob Kaufman Jul 13 '11 at 21:59

1 Answers1

2
Pattern p = Pattern.compile("<!--abc Start-->(.*?)<!--abc end-->", Pattern.DOTALL);
Matcher m = p.matcher(temp);

while(m.find()) {
  String betweenStartAndEnd = m.group(1);
  ...
}
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • Thanks all, for you input, really appreciate your time and help. I basically do get an xslt as a string which will have these .., so I want to pull everything between these as this can be in the string n number of time.s – Nomad Jul 13 '11 at 21:55
  • @Nomad, I think you were on the right path with the `while(m.find())`. The loop above should execute the loop body once for each begin/end pair in the string. – Mike Samuel Jul 13 '11 at 22:03
  • Thanks a Mike for your answer and sorry for the delay reply and acceptance. I really appreciated your help, rather than other folks who were sarcastic and didn't answer the question. Thanks again. – Nomad Jul 28 '11 at 01:46