为什么 replaceAll("$","") 不工作,尽管 replace("$","") 工作正常?
Why replaceAll("$","") is not working although replace("$","") works just fine?
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
int turns;
Scanner scan=new Scanner(System.in);
turns=scan.nextInt();
while(turns-->0)
{
String pattern=scan.next();
String text=scan.next();
System.out.println(regex(pattern,text));
}
}//end of main method
static int regex(String pattern,String text)
{
if(pattern.startsWith("^"))
{
if(text.startsWith(pattern.replace("^","")))
return 1;
}
else if(pattern.endsWith("$"))
{
if(text.endsWith(pattern.replace("$","")))
return 1;
}
else
{
if(text.contains(pattern))
return 1;
}
return 0;
}
}
输入:
2个
或$
阿多尔
或$
艾莉亚
输出:
1个
0
在这个程序中,我正在扫描两个参数(字符串),其中第一个是模式,第二个是我必须在其中找到模式的文本。如果模式匹配,方法应该 return 1 否则 return 0。
使用 replace 时它工作正常但是当我将 replace() 替换为 replaceAll() 时它无法按预期正常工作。
我怎样才能让 replaceAll() 在这个程序中工作。
$
是正则表达式 (EOL) 中的特殊字符。你必须逃脱它
pattern.replaceAll("\$","")
因为replaceAll
需要一个定义正则表达式的字符串,而$
表示正则表达式中的"end of line"。来自 link:
public String replaceAll(String regex,
String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
您需要使用反斜杠对其进行转义(在字符串文字中也必须进行转义):
if(text.endsWith(pattern.replaceAll("\$","")))
对于要逐字替换的复杂字符串,Pattern.quote
很有用:
if(text.endsWith(pattern.replaceAll(Pattern.quote("$"),"")))
此处不需要它,因为您的替换字符串是 ""
,但如果您的替换字符串中可能包含特殊字符(如反斜杠或美元符号),请在替换字符串上使用 Matcher.quoteReplacement
还有。
尽管名称相似,但这是两种截然不同的方法。
replace
用其他子串 (*) 替换子串。
replaceAll
使用正则表达式匹配,$
是那里的一个特殊控制字符(意思是"end of string/line")。
你不应该在这里使用 replaceAll
,但如果你必须,你必须 quote the $
:
pattern.replaceAll(Pattern.quote("$"),"")
(*) 让事情变得更混乱,replace
也取代了 all 的出现,所以只是方法名称的不同并不能全部描述功能的不同.
通过将 $ 替换为 \$ 来引入另一个级别的复杂性。
"$ABC$AB".replaceAll(Matcher.quoteReplacement("$"), Matcher.quoteReplacement("\\$"))
// Output - \$ABC\$AB
这对我有用。
对于此处报告的问题,
"$ABC$AB".replaceAll(Matcher.quoteReplacement("$"), "")
应该可以。
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
int turns;
Scanner scan=new Scanner(System.in);
turns=scan.nextInt();
while(turns-->0)
{
String pattern=scan.next();
String text=scan.next();
System.out.println(regex(pattern,text));
}
}//end of main method
static int regex(String pattern,String text)
{
if(pattern.startsWith("^"))
{
if(text.startsWith(pattern.replace("^","")))
return 1;
}
else if(pattern.endsWith("$"))
{
if(text.endsWith(pattern.replace("$","")))
return 1;
}
else
{
if(text.contains(pattern))
return 1;
}
return 0;
}
}
输入: 2个 或$ 阿多尔 或$ 艾莉亚
输出: 1个 0
在这个程序中,我正在扫描两个参数(字符串),其中第一个是模式,第二个是我必须在其中找到模式的文本。如果模式匹配,方法应该 return 1 否则 return 0。 使用 replace 时它工作正常但是当我将 replace() 替换为 replaceAll() 时它无法按预期正常工作。 我怎样才能让 replaceAll() 在这个程序中工作。
$
是正则表达式 (EOL) 中的特殊字符。你必须逃脱它
pattern.replaceAll("\$","")
因为replaceAll
需要一个定义正则表达式的字符串,而$
表示正则表达式中的"end of line"。来自 link:
public String replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
您需要使用反斜杠对其进行转义(在字符串文字中也必须进行转义):
if(text.endsWith(pattern.replaceAll("\$","")))
对于要逐字替换的复杂字符串,Pattern.quote
很有用:
if(text.endsWith(pattern.replaceAll(Pattern.quote("$"),"")))
此处不需要它,因为您的替换字符串是 ""
,但如果您的替换字符串中可能包含特殊字符(如反斜杠或美元符号),请在替换字符串上使用 Matcher.quoteReplacement
还有。
尽管名称相似,但这是两种截然不同的方法。
replace
用其他子串 (*) 替换子串。
replaceAll
使用正则表达式匹配,$
是那里的一个特殊控制字符(意思是"end of string/line")。
你不应该在这里使用 replaceAll
,但如果你必须,你必须 quote the $
:
pattern.replaceAll(Pattern.quote("$"),"")
(*) 让事情变得更混乱,replace
也取代了 all 的出现,所以只是方法名称的不同并不能全部描述功能的不同.
通过将 $ 替换为 \$ 来引入另一个级别的复杂性。
"$ABC$AB".replaceAll(Matcher.quoteReplacement("$"), Matcher.quoteReplacement("\\$"))
// Output - \$ABC\$AB
这对我有用。
对于此处报告的问题,
"$ABC$AB".replaceAll(Matcher.quoteReplacement("$"), "")
应该可以。