我在 java 中的字符串变量中有以下字符串。
rule "6"
no-loop true
when
then
String prefix = null;
prefix = "900";
String style = null;
style = "490";
String grade = null;
grade = "GL";
double basePrice = 0.0;
basePrice = 837.00;
String ruleName = null;
ruleName = "SIVM_BASE_PRICE_006
Rahul Kumar Singh";
ProductConfigurationCreator.createFact(drools, prefix, style,grade,baseprice,rulename);
end
rule "5"
no-loop true
when
then
String prefix = null;
prefix = "800";
String style = null;
style = "481";
String grade = null;
grade = "FL";
double basePrice = 0.0;
basePrice = 882.00;
String ruleName = null;
ruleName = "SIVM_BASE_PRICE_005";
ProductConfigurationCreator.createFact(drools, prefix, style,grade,baseprice,rulename);
end
我需要用空格替换“THEN”和“END”关键字之间的回车符,使其变得像下面的代码:
rule "6"
no-loop true
when
then
String prefix = null;
prefix = "900";
String style = null;
style = "490";
String grade = null;
grade = "GL";
double basePrice = 0.0;
basePrice = 837.00;
String ruleName = null;
ruleName = "SIVM_BASE_PRICE_006 Rahul Kumar Singh";
ProductConfigurationCreator.createFact(drools, prefix, style,grade,baseprice,rulename);
end
rule "5"
no-loop true
when
then
String prefix = null;
prefix = "800";
String style = null;
style = "481";
String grade = null;
grade = "FL";
double basePrice = 0.0;
basePrice = 882.00;
String ruleName = null;
ruleName = "SIVM_BASE_PRICE_005";
ProductConfigurationCreator.createFact(drools, prefix, style,grade,baseprice,rulename);
end
在上面的两个字符串集示例中,第二个是我需要的正确格式。然而,在第一组中,我得到了这个:
ruleName = "SIVM_BASE_PRICE_006
Rahul Kumar Singh";
具体需要是这样的:
ruleName = "SIVM_BASE_PRICE_006 Rahul Kumar Singh";
而且我还需要确保这不会影响字符串中的任何其他内容。 因此,我需要将这个“回车符”替换为空格并排成一行。这是我的要求。我尝试使用字符串的replace和replaceAll方法,但无法正常工作。
问题:
I need to look in between string "then" and "end" and in that whenever there is any carriage return in between two double quaotes "" ""; I need to replace this carriage return with white space and make it in one line.
谢谢
编辑:
DRT:
template header
Prefix
Style
Product
package com.xx
import com.xx.drools.ProductConfigurationCreator;
template "ProductSetUp"
rule "Product_@{row.rowNumber}"
no-loop true
when
then
String prefix = null;
prefix = "@{Prefix}";
String style = null;
prefix = "@{Style}";
String product = null;
product = "@{Product}";
ProductConfigurationCreator.createProductFact(drools,prefix,style,product);
end
end template
excel和drt仅用于演示目的。 在图像中的“产品”列中,有“SOFAS\rkumar shorav”。实际上这是造成问题的。这将生成如下所示:
product = "SOFAS
kumar shorav";
我需要如下所示:
product = "SOFAS kumar shorav";
然后是 Excel 数据:
附图。
最佳答案
我可能会编写自己的格式化程序,而不是正则表达式
- 检查光标是否在引号内
- 将每个
\r
替换为空格 - 将每个
\n
替换为空格,除非它紧接在\r
之后,这意味着已经为该\r
放置了空格 - 写出其余字符,保持不变。
唯一可能的问题是,此格式化程序不会关心字符串的放置位置,因此如果您想格式化字符串的某些特定部分,则只需提供该部分。
实现此类格式化程序的代码可能如下所示:
public static String format(String text){
StringBuilder sb = new StringBuilder();
boolean insideQuote = false;
char previous = '\0';//to track `\r\n`
for (char ch : text.toCharArray()) {
if (insideQuote &&
(ch == '\r' ||
ch == '\n' && previous != '\r') ) {
sb.append(" ");//replace `\r` or `\n` with space
}else {
if (ch == '"') {
insideQuote = !insideQuote;
}
sb.append(ch); //write other characters without change
}
previous = ch;
}
return sb.toString();
}
辅助实用方法
public static String format(File file, String encoding) throws IOException {
String text = new String(Files.readAllBytes(file.toPath()), encoding);
return format(text);
}
用法:
String formatted = format(new File("input.txt"), "utf-8");
System.out.println(formatted);
关于java - java中用空格替换回车符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29841260/