1

i have an example of the following string (all this in one string).

6 x 9 + 4 = 58.0 

</br> 58.0  markers cost $33.

</br> 33/ 58.0  = 0.5689655172413793 

<br>Each marker cost $0.5689655172413793 .

<br> 0.5689655172413793  x 9 = 5.120689655172414 

<br>Each eraser cost $5.120689655172414 

<br> (5.120689655172414  x 3) +  (0.5689655172413793  x 10) = 21.051724137931036 

<br>The total cost is $21.051724137931036 .

I want to remove all the other stuff and get only the mathematical expressions..

6 x 9 + 4 = 58.0
33/ 58.0  = 0.5689655172413793 
0.5689655172413793  x 9 = 5.120689655172414 
(5.120689655172414  x 3) +  (0.5689655172413793  x 10) = 21.051724137931036 

I have tried the regex pattern to remove all the other stuff but it get splitted indvidually one char by one char.

 Matcher matcher = Pattern.compile("(\\d+|[\\+\\-*/=()])").matcher(qalist.get(i).getFullsol());

         while(matcher.find())
         {
             System.out.println(matcher.group(0));
         }

Anyone can help? :)

Encountered Problem

 Matcher matcher = Pattern.compile("\\(?(\\d+(?:\\.\\d+)?[^=:a-wyz]+)=\\s*(\\d+(?:\\.\\d+)?)").matcher("The expression is (9 * 1) -1 = 8 ");

I will get :

Expression: 9 * 1) -1

Answer: 8

Wonder whats wrong..

Whole expression: (5.120689655172414  x 3) +  (0.5689655172413793  x 10) = 21.051724137931036
Expression: 5.120689655172414  x 3) +  (0.5689655172413793  x 10)
Answer: 21.051724137931036
3
  • Are the <br> tags in the string too? Commented Jan 1, 2014 at 10:36
  • Yup Jerry... the <br> is in the string too. :) Commented Jan 1, 2014 at 10:37
  • What is about read char by char and let thru all but letters, <, >, $ ... Commented Jan 1, 2014 at 10:54

3 Answers 3

1

You could perhaps use this pattern:

\\(?\\d+(?:\\.\\d+)?[^=a-wyz]+=\\s*\\d+(?:\\.\\d+)?

regex101 demo

\\(? is to match a potential opening parenthesis

\\d+(?:\\.\\d+)? is to match a number, integer or floating.

[^=a-wyz]+= will match almost anything except equal sign and letters (x will match however) except letters until it reaches an equal sign.

\\s*\\d+(?:\\.\\d+)? is to match the result of the expression, consisting of potential spaces, and a number (integer or floating).

Sign up to request clarification or add additional context in comments.

20 Comments

Hi Jerry your codes works wonders for most of my answer templates. However i have one answer template that looks like this. I have edited from my post
@rushh05 I cannot see the answer template :(
To Jerry...I have a template that looks like this... It just seem to have the extra 1:, 2:, 3: infront. How do i remove it? :) Step1:7 - 1 = 6 Step2: 6/7*6 = 306 Step 3: 306 - 19 = 287
And the answer shows as..... 1: 7 - 1 = 6 2: 6/7*6 = 306 3: 306 - 19 = 287
@rushh05 Oh! You can add : to the negated class! Change [^=a-wyz] to [^=:a-wyz]. If you encounter similar problems, you could add characters which shouldn't be there inside.
|
0

try this

    StringBuilder sb = new StringBuilder();
    for (String l : str.split("</?br>")) {
        l = l.trim();
        if (!l.isEmpty() && !l.matches(".*[a-zA-Z&&[^x]].*")) {
            sb.append(l).append("\n");
        }
    }

Comments

0

How about:

([()\s\d.x*+/-]+\s*=\s*[\d.]+)

2 Comments

I got error when i put it as ("([()\s\d.x*+/-]+\s*=\s*[\d.]+)")
Its okay M42. :) thanks for the effort. Appreciate it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.