3

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

}
Vivek_Neel
  • 1,343
  • 1
  • 14
  • 25
  • 1
    The requirement is unclear. Don't rely on examples to convey a clear specification. Here are example of clear requirements: *"Replace all '<' that are followed by white space with 'less than'."*, or *"Replace all '<' unless [some specific condition]."* – Rainbolt Jan 06 '16 at 15:47
  • Hi , What do you mean by don't rely on examples? yes , i would like to replace "<" followed by white spaces/no white spaces and some character . e.g < 90 or <90 But i have to consider the text before the "<" as well . there can be a space or may not be – Vivek_Neel Jan 06 '16 at 16:05
  • "don't rely on examples" because your given examples don't cover cases like `<90`. – Andy Turner Jan 06 '16 at 16:11
  • Oh andy . I apologize. I got it. I was thinking that you people might assume that as well on yourself by seeing that I'm getting the input from the user. so there can be many scenario. – Vivek_Neel Jan 06 '16 at 16:14
  • Also, a weak specification like "some character" doesn't help, since `` contains `<` followed by "some character". – Andy Turner Jan 06 '16 at 16:15
  • Okay got it. will try to avoid those things in future :) – Vivek_Neel Jan 06 '16 at 16:16

3 Answers3

1

Based on the examples you have given, it looks like you are trying to replace any < followed by a space (or maybe the end of the string) with less than:

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

Edit, based on comments.

If you want to replace any < followed by optional whitespace then a number:

a.replaceAll("<(?=\s*\d)", "less than");
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1

If you are sure that there is always a space before and after the "less than" sign, then this:

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

can be replaced with:

String test = a.replace(" < ", " less than ");
Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64
1

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

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    Hi there, Thanks so much. it works fine. Yes i need to replace "<" with "less than" only for math exp : a < 90 . There can be a space between "a" and "<" | "<" and "90" – Vivek_Neel Jan 06 '16 at 16:04
  • I do not think you really need to check the left-hand context, but if you need, you can try `.replaceAll("(\\w+)\\s*<\\s*(\\d+)", "$1 less than $2")`. – Wiktor Stribiżew Jan 06 '16 at 16:07
  • Really appreciate your help. Been stuck on this for a while. Thanks so much again. – Vivek_Neel Jan 06 '16 at 16:10
  • Hi again , I was just thinking what if the user types "a < b" ? So i have just extended your regex `String ss = a.replaceAll("<\\s*((\\d+)|(\\D+))", "less than $1"); ` but now it messes up for obvious . I am including \D character class. I don't know how do i avoid this now. – Vivek_Neel Jan 06 '16 at 16:47
  • Glad it worked for you. Please also consider upvoting if my answer proved helpful to you (see [How to upvote on Stack Overflow?](http://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow)). – Wiktor Stribiżew Jan 07 '16 at 08:38
  • Hi @stribizhev again , Please read the edits. I have used your solution for other inputs. everything works fine except `hgsf a< sjdfvh> dasjfh a>jsdhf a a< button and > sjf` Now am replacing > as well with "greater than" Thanks for all the help so far. – Vivek_Neel Jan 07 '16 at 13:46
  • Thanks for the update . Really appreciate it. It works fine now . Just a small change to your code : `(m.group(2).contains("<") ? " less than " : " greater than "); ` ` – Vivek_Neel Jan 08 '16 at 16:02
  • You do not have to use `contains` since it will only contain either `<` or `>`. – Wiktor Stribiżew Jan 08 '16 at 16:09
  • But without using contains. Both < >is getting replaced with greater than. I want < should be replaced with less than not greater than :) – Vivek_Neel Jan 08 '16 at 16:12
  • Ok, it is Java. Use `m.group(2).equals("<")` – Wiktor Stribiżew Jan 08 '16 at 16:18