如何在 RegEx 中添加参数?
How to put an argument in a RegEx?
因此,我尝试在 RegEx 模式 中使用一个参数,但我找不到模式,因为该参数是一个包含在较大字符串中的简单字符串.这是任务本身,我从这个 codingbat.com 中获取的,所以一切都清楚了:
THE Precondition and explanation of the task.
Given a string and a non-empty word string, return a version of the
original String where all chars have been replaced by pluses ("+"),
except for appearances of the word string which are preserved
unchanged.
我的代码:
public String plusOut(String str, String word) {
if(str.matches(".*(<word>.*<word>){1,}.*") || str.matches(".*(<word>.*<word>.*<word>){1,}.*")) {
return str.replaceAll(".", "+"); //after finding the argument I can easily exclude it but for now I have a bigger problem in the if-condition
} else {
return str;
}
}
Java 中是否有匹配参数的方法?由于显而易见的原因 (<word>
),上述代码不起作用。如何在字符串 RegEx 中使用参数 word
?
更新
这是我得到的最接近的,但它仅适用于 字 字符串的最后一个字符。
public String plusOut(String str, String word)
{
if(str.matches(".*("+ word + ".*" + word + "){1,}.*") || str.matches(".*(" + word + ".*" + word + ".*" + word + "){1,}.*") || str.matches(".*("+ word + "){1,}.*"))
{
return str.replaceAll(".(?<!" + word + ")", "+");
} else {
return str;
}
}
Input/Output
plusOut("12xy34", "xy") → "+++y++" (应为 "++xy++")
plusOut("12xy34", "1") → "1+++++" (预期 "1+++++")
plusOut("12xy34xyabcxy", "xy") → "+++y+++y++++y" (预期 "++xy++xy+++xy")
这是因为正则表达式中的 ?。
您需要使用 Java
的 +
运算符连接它
if(str.matches("<"+word+">")){ // Now word will be replaced by the value
//do Anything
}
您不能在正则表达式模式中放置参数。您可以通过将变量与正则表达式模式部分连接起来来创建正则表达式对象,如下所示:
public String plusOut(String str, String word)
{
if(str.matches(".*("+ word + ".*" + word + "){1,}.*") || str.matches(".*(" + word + ".*" + word + ".*" + word + "){1,}.*"))
{
return str.replaceAll(".", "+");
}
else
{
return str;
}
}
你不能只用模式来做,你必须在模式之外写一些代码。试试这个:
public static String plusOut(String input, String word) {
StringBuilder builder = new StringBuilder();
Pattern pattern = Pattern.compile(Pattern.quote(word));
Matcher matcher = pattern.matcher(input);
int start = 0;
while(matcher.find()) {
char[] replacement = new char[matcher.start() - start];
Arrays.fill(replacement, '+');
builder.append(new String(replacement)).append(word);
start = matcher.end();
}
if(start < input.length()) {
char[] replacement = new char[input.length() - start];
Arrays.fill(replacement, '+');
builder.append(new String(replacement));
}
return builder.toString();
}
因此,我尝试在 RegEx 模式 中使用一个参数,但我找不到模式,因为该参数是一个包含在较大字符串中的简单字符串.这是任务本身,我从这个 codingbat.com 中获取的,所以一切都清楚了:
THE Precondition and explanation of the task.
Given a string and a non-empty word string, return a version of the original String where all chars have been replaced by pluses ("+"), except for appearances of the word string which are preserved unchanged.
我的代码:
public String plusOut(String str, String word) {
if(str.matches(".*(<word>.*<word>){1,}.*") || str.matches(".*(<word>.*<word>.*<word>){1,}.*")) {
return str.replaceAll(".", "+"); //after finding the argument I can easily exclude it but for now I have a bigger problem in the if-condition
} else {
return str;
}
}
Java 中是否有匹配参数的方法?由于显而易见的原因 (<word>
),上述代码不起作用。如何在字符串 RegEx 中使用参数 word
?
更新
这是我得到的最接近的,但它仅适用于 字 字符串的最后一个字符。
public String plusOut(String str, String word)
{
if(str.matches(".*("+ word + ".*" + word + "){1,}.*") || str.matches(".*(" + word + ".*" + word + ".*" + word + "){1,}.*") || str.matches(".*("+ word + "){1,}.*"))
{
return str.replaceAll(".(?<!" + word + ")", "+");
} else {
return str;
}
}
Input/Output
plusOut("12xy34", "xy") → "+++y++" (应为 "++xy++")
plusOut("12xy34", "1") → "1+++++" (预期 "1+++++")
plusOut("12xy34xyabcxy", "xy") → "+++y+++y++++y" (预期 "++xy++xy+++xy")
这是因为正则表达式中的 ?。
您需要使用 Java
的+
运算符连接它
if(str.matches("<"+word+">")){ // Now word will be replaced by the value
//do Anything
}
您不能在正则表达式模式中放置参数。您可以通过将变量与正则表达式模式部分连接起来来创建正则表达式对象,如下所示:
public String plusOut(String str, String word)
{
if(str.matches(".*("+ word + ".*" + word + "){1,}.*") || str.matches(".*(" + word + ".*" + word + ".*" + word + "){1,}.*"))
{
return str.replaceAll(".", "+");
}
else
{
return str;
}
}
你不能只用模式来做,你必须在模式之外写一些代码。试试这个:
public static String plusOut(String input, String word) {
StringBuilder builder = new StringBuilder();
Pattern pattern = Pattern.compile(Pattern.quote(word));
Matcher matcher = pattern.matcher(input);
int start = 0;
while(matcher.find()) {
char[] replacement = new char[matcher.start() - start];
Arrays.fill(replacement, '+');
builder.append(new String(replacement)).append(word);
start = matcher.end();
}
if(start < input.length()) {
char[] replacement = new char[input.length() - start];
Arrays.fill(replacement, '+');
builder.append(new String(replacement));
}
return builder.toString();
}