I don't know Java very well, but I do know regular expressions, C# and JavaScript. I am confident you can adapt one of my techniques to Java.
I have sample code with two different techniques.
- The first invokes a function on every match to perform the replacement
- The second iterates the matches provided by your regular expression you and convert each match into Roman numerals, then injects the result into your original text.
The link below illustrates technique 1 using DotNetFiddle. The replacement function takes a method name. The method in question performs is invoked for every match. This technique requires very little code.
https://dotnetfiddle.net/o9gG28. If you're lucky, Java has a similar technique available.
Technique 2: a javascript version that loops through every match found by the regex:
https://jsfiddle.net/ActualRandy/rxnzoc3u/81/. The method does some string concatenation using the replacement value.
Here's some code for method 2 using .NET syntax, Java should be similar. The key methods are 'Match' and 'GetNextMatch'. Match uses your regex to get the first match.
private void btnRegexRep_Click(object sender, RoutedEventArgs e) {
string fixThis = @"Hans4444müller,Mary555kren";
var re = new Regex("\\d+");
string result = "";
int lastIndex = 0;
string lastMatch = "";
//Get the first match using the regular expression:
var m = re.Match(fixThis);
//Keep looping while we can match:
while (m.Success) {
//Get length of text between last match and current match:
int len = m.Index - (lastIndex + lastMatch.Length);
result += fixThis.Substring(lastIndex + lastMatch.Length, len) + GetRomanText(m);
//Save values for next iteration:
lastIndex = m.Index;
lastMatch = m.Value;
m = m.NextMatch();
}
//Append text after last match:
if (lastIndex > 0) {
result += fixThis.Substring(lastIndex + lastMatch.Length);
}
Console.WriteLine(result);
}
private string GetRomanText(Match m) {
string[] roman = new[] { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "IX" };
string result = "";
// Get ASCII value of first digit from the match (remember, 48= ascii 0, 57=ascii 9):
char c = m.Value[0];
if (c >= 48 && c <= 57) {
int index = c - 48;
result = roman[index];
}
return result;
}