Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check sentence against the given regex and replace "<" with "less than" word

Pls read the comments in the code. Different scenarios ...it would be great if any one could tell me other solutions to achieve the same. Pls read the comments in the code. Different scenarios ...it would be great if any one could tell me other solutions to achieve the same.

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

}
like image 868
Vivek_Neel Avatar asked Dec 28 '25 18:12

Vivek_Neel


1 Answers

Assuming you can have whitespace symbols around < and the substrings inside <> can also have whitespace inside (like < play >), you can use

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

And replace with $1 less than $2. The regex matches...

  • (\w+) - (Group 1) one or more alphanumeric and underscore characters
  • \s* - zero or more whitespace
  • < - a literal < character
  • `\s* - zero or more whitespace
  • (\w++(?!\s*>)) - 1 or more word characters that are not followed by optional whitespace(s) and a closing >. Note that the ++ possessive quantifier is very important since it switches off backtracking and only enforces the lookahead to be run after the last word character found with \w++.

See IDEONE demo:

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

Results:

<play> a less than b  <play> at < button >
<play> a less than 90 <play> at < button >

UPDATE

For the greater than use

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

See another demo

like image 103
Wiktor Stribiżew Avatar answered Dec 31 '25 11:12

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!