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

4

4 回答 4

2

要匹配数字,请使用数字后跟可选的 dot-then-digit(s),前面是一个左括号:

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

该表达式(?<=\()向后看,表示前面的 char 必须是(,但不会将它作为匹配的一部分使用。

同样,(?=g\))断言下一个字符是g). 您可能不需要(?=g\)),但它会使比赛更加紧张。

于 2018-06-24T23:59:55.600 回答
0

到目前为止,除了我尝试过的数百个之外,此页面上所有非常友好的建议都产生6521 3.2 345 8 34了数据字符串中的所有数字,而不仅仅是介于(and之间的那些g) 只会导致3.2&8

\d+.\d+|\d+

\d+(\.\d)?

\d+(\.\d+)?

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

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

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

也许不幸的是不可能的,或者也许,尽管我一直认为它非常先进,但 Tasker 不喜欢它。

如果任何正则表达式向导想要给这个 30 秒的破解,我将永远感激不尽。

再次感谢!

于 2018-06-25T13:05:00.637 回答
0

这是一个如何执行此操作的示例:

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

    }
}

您可以替换(3.2g) (8g)为您的String值。如果需要,您还可以将列表转换为数组。

于 2018-06-24T23:59:24.237 回答
0

请尝试以下模式: (?<=\()\d+\.?\d?(?=g\))

在线演示

于 2018-06-24T23:48:23.043 回答