簡體   English   中英

檢查給定正則表達式的句子,並將“<”替換為“小於”單詞

[英]Check sentence against the given regex and replace “<” with “less than” word

請閱讀代碼中的注釋。 不同的場景......如果任何人能告訴我其他解決方案來實現同樣的目標,那就太棒了。 請閱讀代碼中的注釋。 不同的場景......如果任何人能告訴我其他解決方案來實現同樣的目標,那就太棒了。

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

}

根據您給出的示例,您似乎嘗試用less than :替換任何<后跟空格(或可能是字符串的結尾):

a.replaceAll("<(?= |$)", "less than");

根據評論進行修改。

如果你想替換任何<后跟可選的空格,那么一個數字:

a.replaceAll("<(?=\s*\d)", "less than");

如果您確定在“小於”符號之前和之后總是有空格,那么:

String test = a.replace("<", "less than");

可以替換為:

String test = a.replace(" < ", " less than ");

假設你可以在<周圍有空格符號,並且<>里面的子串也可以在里面有空格(比如< play > ),你可以使用

(\w+)\s*<\s*(\w++(?!\s*>))

並以$1 less than $2替換。 正則表達式匹配......

  • (\\w+) - (組1)一個或多個字母數字和下划線字符
  • \\s* - 零個或多個空格
  • < - 文字<字符
  • `\\ s * - 零個或多個空格
  • (\\w++(?!\\s*>)) - 一個或多個單詞字符,后面沒有可選的空格和結束> 請注意++占有量詞非常重要,因為它會關閉回溯並且只強制在使用\\w++找到最后一個單詞字符運行前瞻。

請參閱IDEONE演示

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 >

UPDATE

對於greater than使用

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());

另一個演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM