Solved

Reg expression gurus

Posted on 2004-04-05
4
364 Views
Last Modified: 2010-03-31
I want to get every href in page into array.
So <A href= "Link1">Link1</A><BR><A href= "Link2">Link2<H3><A name="Link3">Link3</A></H3>
I want 3 ref to Link1, Link2, and Link3
Any reg exp that can do this?
0
Comment
Question by:Paullkha
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Thandava Vallepalli
Comment Utility
Hello,

http://www.regular-expressions.info/examples.html

Eg:

"<a[^>]*>(.*?)</a>"

All the best,
V.Thandava Krishna.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You need to make that regex part of a case-insensitive match or it won't work

final String RE_LINK = "<a[^>]*>(.*?)</a>";
Pattern pattern = Pattern.compile(RE_LINK, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(yourString);
0
 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 125 total points
Comment Utility
You can do it in 2 steps: split and replaceFirst

    String splitRegExp   = "<[A-a] [^>]*>";
    String matchRegExp   = "</[A-a]>.*";
    String replaceRegExp = "";

    String[] splitStrings = (test).split(splitRegExp);
    String[] anchorStrings = new String[splitStrings.length - 1];
    for (int i = 1; i < splitStrings.length; i++) {
      anchorStrings[i - 1] = splitStrings[i].replaceFirst(matchRegExp, replaceRegExp);
    }
    System.out.println(Arrays.asList(anchorStrings).toString());
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
:°)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now