-3

I need to write a regular expression in order to use it in java using pattern and matcher. the regular expression should test if a word inside a text is surrounded by javascript tags. For examples, lets say i have the following text:

**testtesttesttest <script> example of a "text" </script> testtesttesttest**

and i need to check if the word "text" is surrounded by <script> and </script>.

I have split the text to have two texts:

txt1 = testtesttesttest <script> example of a 
txt2 = </script> testtesttesttest

Now i need to check:

  1. if txt1 contains the tag <script> but does not contain after it a tag </script>
  2. if txt2 contains the tag </script> but does not contain before it a tag <script>

Is there a way to do it using the regular expression? Thank you in advance,

VLAZ
  • 26,331
  • 9
  • 49
  • 67
user2318955
  • 95
  • 10
  • 6
    Yes, there is a way to do it using regular expressions. – ElGavilan Jul 15 '14 at 13:21
  • 3
    Using regex to parse HTML is never a good idea: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – beny23 Jul 15 '14 at 13:23
  • Parsing HTML with regular expression is not a good idea – Nadendla Jul 15 '14 at 13:24
  • 2
    For the described problem a regular expression is just fine and will do the job. It is not a context sensitive problem (where a push down automaton would be necessary) to check whether there's one opening and one closing surrounding tag! – Kalle Richter Jul 15 '14 at 13:30

1 Answers1

0

First you can get inside of your javascript tag with regular expessions.

final Pattern pattern = Pattern.compile("<script>(.+?)</script>");
    final Matcher matcher = pattern.matcher("**testtesttesttest <script> example of a \"text\" </script> testtesttesttest**");
    matcher.find();
    String insdeOfTag= matcher.group(1);

Then, you can check if string contains "text"

insdeOfTag.contains("text");

You can loop all the results with this:

List<String> values = new ArrayList<String>();
while (matcher.find()) {
        values.add(matcher.group(1));
}

The values list will contain the texts inside the javascript tags.

for (String text : values) {
//you can look for the word "text" on a specific index in here.
}
user2640782
  • 1,074
  • 8
  • 15
  • Thank you, but the text might contain multiple word "text" inside script tags, and in my case i need to do the check on a word "text" on a specific index, so i will need to loop all the results of matcher and check if the word "text" has the same index. – user2318955 Jul 15 '14 at 14:10