1

Sorry for being a noob but I have been searching the forums for hours but no luck so huge thanks if you can help.

I'm using Tasker which I believe uses a java flavor of regex.

I have random data for example: blah blah blah 6521 (3.2g) 345 (8g) 34 etc...

Between the brackets there's only ever a just a single digit or a single digit followed by a single decimal digit, with the ( and g) always being constant, I simply need to extract numbers like:

3.2 & 8 into their array which Tasker will do.

What I would think should work, is not working properly:

\d+.\d+|\d+

It produces all the numbers instead of just those between ( and g)

Any pointers anyone?

Much appreciated all!

EDIT:typos

binvius
  • 31
  • 6
  • I think you have to escape the parenthesis, they're grouping operators otherwise. – markspace Jun 24 '18 at 23:29
  • Make the `.` and trailing number optional, `\d+(\.\d)?`. I don't know java but that regex should capture the numerical values you want. – user3783243 Jun 25 '18 at 00:26
  • Thanks but unfortunatly this produces other numbers that arent in brackets with 'g'. I have edited the orginal data for clarification. Cheers again! – binvius Jun 25 '18 at 09:08

4 Answers4

2

To match the numbers, use digit(s) folowed by optional dot-then-digit(s), preceded by an open bracket:

(?<=\()\d+(\.\d+)?(?=g\))

The expression (?<=\() is a look behind and means the preceding char must be (, but doesn’t consume the it as part of the match.

Similarly, (?=g\)) asserts the the next chars are g). You probably don’t need (?=g\)), but it tightens up the match.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Thanks but unfortunatly this produces other numbers that arent in brackets with 'g'. I have edited the orginal data for clarification. Cheers again! – binvius Jun 25 '18 at 09:10
0

Please try the following pattern: (?<=\()\d+\.?\d?(?=g\))

Online Demo

Community
  • 1
  • 1
Quinn
  • 4,394
  • 2
  • 21
  • 19
  • Thanks but unfortunatly this produces other numbers that arent in brackets with 'g'. I have edited the orginal data for clarification. Cheers again! – binvius Jun 25 '18 at 09:12
  • Hi @binvius: I have updated the pattern based on your sample input. Please check if it fits your needs. – Quinn Jun 25 '18 at 13:38
  • Quinn, you beautiful beautiful beast - you've only gone and cracked it! Dozens of mere mortals have fought but only you have conquered. I cannot thank you enough for doing a lovely thing for a random stranger. Keep up the good work and all the best for the future – binvius Jun 25 '18 at 21:09
0

Here's an example of how you can do this:

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.*; 

public class YourClass {
    public static void main(String args[]) {
        /* Create a list to store the result */
        List<String> allMatches = new ArrayList<String>();
        Matcher m = Pattern.compile("(\\d+).(\\d+)|(\\d+)").matcher("(3.2g) (8g)");

        /* Add the results to list */
        while (m.find()) {
           allMatches.add(m.group());
        }

        /* Print out the result */
        for(int i=0; i< allMatches.size(); i++) {
            System.out.print(allMatches.get(i)+" ");
        }

    }
}

You can replace (3.2g) (8g) with your String value. You can also cast list into array, if needed.

dmitryro
  • 3,463
  • 2
  • 20
  • 28
  • Cheers for this. I tried just the regex part but with no luck. With many more hours trying, Im starting to think that this may not even be possible with regex. Thanks anyone else if you can help. – binvius Jun 25 '18 at 11:54
0

So far and aside from the hundreds I've tried, all of the very kind suggestions on this page have produced all the numbers 6521 3.2 345 8 34 from the data string, instead of just those between ( and g) which would only result in3.2 & 8

\d+.\d+|\d+

\d+(\.\d)?

\d+(\.\d+)?

\d+\.\d+|\d+

("(\\d+).(\\d+)|(\\d+)")

(\\d+).(\\d+)|(\\d+)

Perhaps it is unfortunately not possible or maybe, although I always believed it was very advanced, Tasker doesn't like something about it.

If any Regex wizards feel like giving this a 30 second crack, I would be eternally grateful.

Thanks again!

binvius
  • 31
  • 6
  • This should be an edit to your original post, not an answer. It doesn't seem to answer your question and is buried under other people's answers :) – James Whiteley Jun 25 '18 at 13:40