Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract HTML tags from String using Regular Expression

Status
Not open for further replies.

gorgered

Programmer
Dec 13, 2005
24
US
Hi,

I am very new to regular expressions and I need some of your help.

Code:
String htmlString = "The values for Key1 is <span id=\"key1\">value1</span> and for Key2 is <span id=\"key2\">Value2</span>.";

Output form the reg exp should be the html tags.

key1:value1
key2:value2

I looked at the jakarta-oro and it looks good but as I said I am just a starter.

Thanks
Gorge


 
Hi,

Here her the example for regexp hope this will hlep you

Code:
  public void extractIdValue() {
        String htmlString = "The values for Key1 is <span id=\"key1\">value1</span> and for Key2 is <span id=\"key2\">Value2</span>.";
        String regexpForSpanTag = "<span\\s*id=\"\\s*([^\"]+)\\s*\"\\s*>([^*<]+)</span>";
        Pattern pattern = Pattern.compile(regexpForSpanTag);
        Matcher matcher = pattern.matcher(htmlString);
        while (matcher.find()) {
            String spanTag = matcher.group();
//            System.out.println("Tag is " + spanTag);
            System.out.println("" + matcher.group(1) + ":" + matcher.group(2));
        }
    }

Here are some links that will hlep you

Cheers
-Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top