May 2009
Intermediate to advanced
510 pages
15h
English
The previous recipe shows how a regex could be applied repeatedly to a string to get a list of matches. Now you want to iterate over all the matches in your own code.
You can use the static call when you process only a small number of strings with the same regular expression:
Match matchResult = Regex.Match(subjectString, @"\d+");
while (matchResult.Success) {
// Here you can process the match stored in matchResult
matchResult = matchResult.NextMatch();
}Construct a Regex object if you want to use the same
regular expression with a large number of strings:
Regex regexObj = new Regex(@"\d+");
matchResult = regexObj.Match(subjectString);
while (matchResult.Success) {
// Here you can process the match stored in matchResult
matchResult = matchResult.NextMatch();
}You can use the static call when you process only a small number of strings with the same regular expression:
Dim MatchResult = Regex.Match(SubjectString, "\d+")
While MatchResult.Success
'Here you can process the match stored in MatchResult
MatchResult = MatchResult.NextMatch
End WhileConstruct a Regex object if you want to use the same
regular expression with a large number of strings:
Dim RegexObj As New Regex("\d+")
Dim MatchResult = RegexObj.Match(SubjectString)
While MatchResult.Success
'Here you can process the match stored in MatchResult
MatchResult = MatchResult.NextMatch
End WhilePattern regex = Pattern.compile("\\d+"); Matcher regexMatcher = regex.matcher(subjectString); ...Read now
Unlock full access