简体   繁体   中英

Java regex - Extract all float numbers before specific unit in String

I am a newbie in java regex. I would like to know how to extract numbers or float numbers before % . For example:

"Titi 10% Toto and tutu equals 20X"
"Titi 10.50% Toto and tutu equals 20X"
"Titi 10-10.50% Toto and tutu equals 20X
"Titi 10sd50 % Toto and tutu equals 20X
"Titi 10-10.50% or 10sd50 % Toto and tutu equals 20X

Output :

10
10.50
10-10.50
10sd50
10-10.50;10sd50

My idea is to replace all before and after "space + number(% or space%)" by ; in order to extract all values or group values before % . I tried to use that: replaceAll("[^0-9.]+|\\\\.(?!\\\\d)(?!\\\\b)\\\\%",";"); = NO SUCCESS

How can I do it?

This one should do the job:

((?:\d+(?:+|-|sd))?\d+(?:\.\d+)\h*%)

Explanation:

(               : start group 1
  (?:           : start non capture group
    \d+         : 1 or more digits
    (?:+|-|sd)  : non capture group that contains + or - or sd
  )?            : end group
  \d+           : 1 or more digits
  (?:           : start non capture group
    \.          : a dot
    \d+         : 1 or more digits
  )             : end group
  \h*           : 0 or more horizontal spaces
  %             : character %
)               : end of group 1

The result will be in group 1.

In java you have to double escape, I've not done it here for readability.

You can do as follows:

  • First find all the matches in each string
  • Replace the last character( % ) of each match elements with Blank
  • Do as your own formatting.

A java samples is given :

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

public class Main {

    public static void main(String[] args) {
        final String regex = "\\d+(\\.?\\d+)?(\\+|\\-|sd)?(\\d+(\\.?\\d+)?)?[ ]*%";
        final String test_str = "\"Titi 10% Toto and tutu equals 20X\"\n"
                + "\"Titi 10.50% Toto and tutu equals 20X\"\n"
                + "\"Titi 10-10.50% Toto and tutu equals 20X\n"
                + "\"Titi 10sd50 % Toto and tutu equals 20X\n"
                + "\"Titi 10-10.50% or 10sd50 % Toto and tutu equals 20X";

        final Pattern pattern = Pattern.compile(regex);
        for(String data : test_str.split("\\r?\\n")) {
            Matcher matcher = pattern.matcher(data);
            while (matcher.find()) {
                System.out.print(data.substring(matcher.start(), matcher.end()-1) + " ") ;
            }
            System.out.println();
        }
    }
}

The above code gives :

10 
10.50 
10-10.50 
10sd50  
10-10.50 10sd50 

You can do anything with these data. You can see the Explanations : Regex101

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM