在 java 中将马车 return 替换为白色 space

replacing the carriage return with white space in java

我在 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

我需要用白色 space 替换 "THEN" 和 "END" 关键字之间的回车 return ,这样它就变成如下代码:

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";

而且我还需要确保这不会影响字符串中的任何其他内容。 因此我需要用白色 space 替换这个 "carriage return" 并在一行中制作。这是我的要求。我尝试使用字符串的 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数据:

附上图片。

使用"multi line"标志:

str = str.replaceAll("(?m)^\s+", "");

多行标志 (?m) 使 ^$ 匹配每行的 start/end(而不是输入的 start/end)。 \s+ 表示 "one or more whitespace characters".

s = s.replaceAll("(?m)^([^\"]*(\"[^\"]*\")*[^\"]*\"[^\"]*)\r?\n\s*", " ");

这会将带有不成对引号的行替换为带有替换行结尾的行。

^.... means starting at the line begin
[^\"] means not quote
\r?\n catches both CR+LF (Windows) as LF (Rest) line endings

not-quotes,
    repetition of " not-quotes ",
    not quotes, quote, not-quotes, newline

注意这不包含反斜杠+引号,它们会自行转义。

我可能会编写自己的格式化程序而不是正则表达式,它将

  • 检查光标是否在引号内
  • 将每个 \r 替换为 space
  • 将每个 \n 替换为 space,除非它紧跟在 \r 之后,这意味着 space 已经被放置在那个 \r
  • 其余字符不变。

唯一可能的问题是此格式化程序不会关心字符串的放置位置,因此如果您想格式化字符串的某些特定部分,您只需提供该部分。

实现此类格式化程序的代码如下所示:

public static String format(String text){

    StringBuilder sb = new StringBuilder();
    boolean insideQuote = false;
    char previous = '[=10=]';//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);

您可能会说 org.drools.template.parser.StringCell 方法

中存在错误
public void addValue(Map<String, Object> vars) {
    vars.put(column.getName(), value);
}

这里,值作为字符串添加到映射中,但这没有考虑到字符串值通常会扩展为字符串文字。因此,嵌入式换行符应转换为转义序列 \n。您可以试试这个补丁:

public void addValue(Map<String, Object> vars) {
    String h = value.replaceAll( "\n", "\\n" );
    vars.put(column.getName(), h);
}

获取源文件,将其放入合适的子目录,将其编译为class 文件,并确保根目录在class 路径中的drools-templates-6.2.0.Final-sources.jar 之前。然后你应该看到

ruleName = "SIVM_BASE_PRICE_006\nRahul Kumar Singh";

在生成的 DRL 文件中。显然,这不是 space、,而是电子表格单元格中写的内容!

我建议(紧急)您不要采用这种方法。原因很简单,字符串 并不总是 在引号之间扩展,然后替换几乎肯定会导致无效代码。根本没有补救措施,因为模板编译器 "dumb" 并没有真正 "know" 它正在扩展什么。

如果电子表格中的字符串包含换行符,模板扩展 必须 忠实地呈现它,并在那儿换行。如果这产生无效 (Java) 代码:为什么首先输入换行符?如果您想要的话,绝对没有理由在那个单元格中有一个space。