Dorka
Dorka

Reputation: 196

Matching a string to a regex from html input

I'm having a little trouble figuring out what to do.

Basically using java I'm trying to:

The first and last steps are simple for me but I'm having no luck (and never had with regex).

I believe this is the beginning of what I need:

   String regex = "(?<=title=\")\\S+";
   Pattern name = Pattern.compile(regex);

After that I have no clue. Any help?

Upvotes: 0

Views: 114

Answers (2)

Ωmega
Ωmega

Reputation: 43663

import java.util.regex.Matcher;
import java.util.regex.Pattern;

String EXAMPLE_TEST = "......";
Pattern pattern = Pattern.compile("(?<=title=\")(\\S+)")
Matcher matcher = pattern.matcher(EXAMPLE_TEST);
while (matcher.find()) {
  System.out.println(matcher.group());
}

Note: You might consider to use regex pattern (?<=title=\")([^\"]*)

Upvotes: 1

Alex W
Alex W

Reputation: 38173

List<String> result_list = new ArrayList<String>();
Pattern p = Pattern.compile("title=\"(.*)\"");
Matcher m = p.matcher("title=\"test\"");
boolean result = m.find();

while(result)
{
    result_list.add(m.group(0));
    result = m.find();
}

Upvotes: 0

Related Questions