Java 对照给定的正则表达式检查句子,并替换“<&引用;加上;少于;单词
请阅读代码中的注释。不同的场景…如果有人能告诉我其他解决方案来实现同样的效果,那就太好了。 请阅读代码中的注释。不同的场景…如果有人能告诉我其他解决方案来实现同样的效果,那就太好了Java 对照给定的正则表达式检查句子,并替换“<&引用;加上;少于;单词,java,regex,replace,char,Java,Regex,Replace,Char,请阅读代码中的注释。不同的场景…如果有人能告诉我其他解决方案来实现同样的效果,那就太好了。 请阅读代码中的注释。不同的场景…如果有人能告诉我其他解决方案来实现同样的效果,那就太好了 package parsexml.matcher; import java.util.Scanner; public class BackUp { // 1. input : if the quantity a < 90 and b > 90 then click on < ok &g
package parsexml.matcher;
import java.util.Scanner;
public class BackUp {
// 1. input : if the quantity a < 90 and b > 90 then click on < ok >
// 1. output :if the quantity a less than 90 and b greater than 90 then click on < ok >
// 2.if the quantity a > 90 or a < 90 then click on < submit>
// 2. output : if the quantity a greater than 90 or a less than 90 then click on < submit>
// etc .from 3- 9 , i get the expected output
// 3. if the quantity a> b then click on <submit>
// 4. if the quantity a > b or a < c then click on < submit>
// 5. if the quantity a < 90 then click on <submit>
// 6. if the quantity a > 90 then click on <submit>
// 7. if the quantity a < b then click on <submit>
// 8. if the quantity a > b then click on < submit >
//9. validate a < 100 in the expression and press < click >
// 10. if amount < fd then if price > 80 click on < submit >
public static void main(String[] arg) {
String inputText;
String outputText = "";
String greater = "";
Scanner s = new Scanner(System.in);
inputText = s.nextLine();
if (inputText.contains("<")) {
outputText = inputText.replaceAll("(\\w+)\\s*<\\s*(\\w++(?!\\s*>))", "$1 less than $2");
// System.out.print("\n"+outputText);
}
if (outputText.contains(">")) {
greater = outputText.replaceAll("(\\w+)\\s*>\\s*(\\w++(?!\\s*>))", "$1 greater than $2");
System.out.print("\n" + greater);
}
if (outputText.contains(">"))
return;
else if (inputText.contains(">")) {
String greater2;
greater2 = inputText.replaceAll("(\\w+)\\s*>\\s*(\\w++(?!\\s*>))", "$1 greater than $2");
System.out.print("\n" + greater2);
} else
return;
}
}
package parsexml.matcher;
导入java.util.Scanner;
公共类备份{
//1.输入:如果数量a<90且b>90,则单击
//1.输出:如果数量a小于90,b大于90,则单击
//2.如果数量a>90或a<90,则点击
//2.输出:如果数量大于90或小于90,则单击
//从3到9,我得到了预期的输出
//3.如果数量a>b,则单击
//4.如果数量a>b或a
//5.如果数量a<90,则单击
//6.如果数量a>90,则单击
//7.如果数量ab,则单击
//9.在表达式中验证<100,然后按
//10.如果金额80,请点击
公共静态void main(字符串[]arg){
字符串输入文本;
字符串outputText=“”;
字符串“=”;
扫描仪s=新的扫描仪(System.in);
inputText=s.nextLine();
if(inputText.contains(“”){
大于等于outputText.replaceAll(“(\\w+\\s*>\\s*”(\\w++(?!\\s*>)”,“$1大于$2”);
系统输出打印(“\n”+更大);
}
if(outputText.contains(“>”)
返回;
else if(inputText.contains(“>”){
字符串更大2;
greater2=inputText.replaceAll(“(\\w+)\\s*>\\s*(\\w++(?!\\s*>)”,“$1大于$2”);
系统输出打印(“\n”+2);
}否则
返回;
}
}
根据您给出的示例,如果您确定“小于”符号前后始终有空格,则看起来您正在尝试替换任何,然后:
String test = a.replace("<", "less than");
String test=a.replace(“假设
),则可以使用
(\w+)\s*<\s*(\w++(?!\s*>))
结果:
<play> a less than b <play> at < button >
<play> a less than 90 <play> at < button >
请参见要求不明确。不要依靠示例来传达明确的规范。以下是明确要求的示例:“全部替换”嗨,你说的“不依赖示例”是什么意思?是的,我想替换“不依赖示例”“因为您给出的示例没有涵盖像噢,andy这样的情况。我很抱歉。我知道了。我在想,如果看到我从用户那里得到了输入,您可能也会认为这一点。因此可能会有很多场景。此外,像“某个角色”这样的弱规范”没有帮助,因为
包含您好,非常感谢。它工作正常。是的,我需要替换“我认为您确实不需要检查左侧上下文,但如果需要,您可以尝试.replaceAll(\\w+)真的很感谢你的帮助。坚持了一段时间。非常感谢。再次,我在想如果用户键入“A < B”怎么办?所以我刚刚扩展了你的正则表达式>代码Ss= A.RePATALL(“很高兴为你工作。如果我的答案证明对你有用的话,请考虑一下投票。”。谢谢。但是在“您的意思是,可能有一个类似于
(带空格)的标记”之前或之后可以有空格?因为我上面的版本保留了现有的空格,所以它不会转换前后没有空格的事件。嘿,没错。对于不正确的要求,我深表歉意。
String test = a.replace(" < ", " less than ");
(\w+)\s*<\s*(\w++(?!\s*>))
String str = " <play> a < b <play> at < button >\n <play> a < 90 <play> at < button >";
System.out.println(str.replaceAll("(\\w+)\\s*<\\s*(\\w++(?!\\s*>))", "$1 less than $2"));
<play> a less than b <play> at < button >
<play> a less than 90 <play> at < button >
String str = " <play> a < b <play> at < button >\n <play> a < 90 <play> at < button >\nhgsf a< sjdfvh> dasjfh a>jsdhf a<fan> a< button and > sjf";
StringBuffer result = new StringBuffer();
Matcher m = Pattern.compile("(\\s*<\\s*\\w+\\s*>\\s*)|\\s*([<>])\\s*").matcher(str);
while (m.find()) {
String replacement = m.group(1) != null ? // Check if Group 1 is matched
m.group(1) : //If yes, use Group 1
(m.group(2).equals("<") ? " less than " : " greater than "); // If not, replace Group 2
m.appendReplacement(result, replacement); // Add the replacement
}
m.appendTail(result);
System.out.println(result.toString());