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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Pattern to find entities

Status
Not open for further replies.

DrHyper

Programmer
Apr 23, 2003
7
0
0
DK
[ignore]How do I modify the code (or the reg. expression) below, so that I get :
groupStr0 = "
groupStr1 = &
groupStr2 = '
groupStr3 = <

instead of only:
groupStr0 = "

*************************************************'

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

public class PatternTest {
public static void main(String[] args) {
String testData = "" tada dum di & dum ' dum; < dum dum tada";
Pattern p = Pattern.compile("(&\\w+;)");
Matcher matcher = p.matcher(testData);
boolean matchFound = matcher.find();

if (matchFound) {
// Get all groups for this match
for (int i = 0; i < matcher.groupCount(); i++) {
String groupStr = matcher.group(i);
System.out.println("groupStr" + i + " = " + groupStr);
}
}
}
}
[/ignore]
 
...well my entities are obv. shown as ",&,' and <

..new example:
How do I modify the code (or the reg. expression) below, so that I get :
groupStr0 = &a;
groupStr1 = &bb;
groupStr2 = &ccc;
groupStr3 = &dddd;

instead of only:
groupStr0 = &a;
*****************************

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

public class PatternTest2 {
public static void main(String[] args) {
String testData = "&a; tada dum di &bb; dum &ccc; dum; &dddd; dum dum tada";
Pattern p = Pattern.compile("(&\\w+;)");
Matcher matcher = p.matcher(testData);
boolean matchFound = matcher.find();

if (matchFound) {
// Get all groups for this match
for (int i = 0; i < matcher.groupCount(); i++) {
String groupStr = matcher.group(i);
System.out.println("groupStr" + i + " = " + groupStr);
}
}
}
}
 
doh! found the solution:

int i = 0;

while (matcher.find()) {
System.out.println("groupstr" + i + " = " + matcher.group());
i++;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top