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

B.Gees

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?

sunkuet02

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

RegEx extract all real numbers from a string

From Dev

Regex for android java to extract the numbers and specific symbols

From Java

Java Regex to extract numbers and strings from a string

From Dev

Java Regex to extract numbers and strings from a string

From Dev

Regex extract all numbers specific lenght starting from last

From Dev

Regex - Negate a specific string / sequence of numbers [java]

From Dev

Regex - Negate a specific string / sequence of numbers [java]

From Dev

Regex to extract a specific string

From Dev

regex to extract first series of numbers in a string and all words after

From Dev

Regex: Extract all elements that contain a that contain a specific letter from a string

From Dev

Regex for extract numbers and extension of string

From Dev

Extract numbers from a url's string group using regex in java

From Dev

java regex to extract 'only' the single digit numbers from a string

From Dev

round all float numbers in a string

From Dev

Extract all numbers in string to array

From Dev

Regex extract string in java

From Dev

Regex PHP remove all float numbers

From Dev

extract numbers before search string in a file

From Dev

Extract all characters before a period with HiveQL regex?

From Dev

Java Regex to extract specific words

From Dev

Using regex extract all digit and word numbers

From Dev

Php: regex get all numbers before colon

From Dev

Java : Extract numbers from a string

From Dev

Java : Extract numbers from a string

From Dev

Extract numbers from a string in java

From Dev

R regex extract number after/before string

From Dev

Can Regex Extract Multiple Numbers from String?

From Dev

Using regex to extract numbers and symbols from string

From Dev

extract numbers within a string using regex

Related Related

  1. 1

    RegEx extract all real numbers from a string

  2. 2

    Regex for android java to extract the numbers and specific symbols

  3. 3

    Java Regex to extract numbers and strings from a string

  4. 4

    Java Regex to extract numbers and strings from a string

  5. 5

    Regex extract all numbers specific lenght starting from last

  6. 6

    Regex - Negate a specific string / sequence of numbers [java]

  7. 7

    Regex - Negate a specific string / sequence of numbers [java]

  8. 8

    Regex to extract a specific string

  9. 9

    regex to extract first series of numbers in a string and all words after

  10. 10

    Regex: Extract all elements that contain a that contain a specific letter from a string

  11. 11

    Regex for extract numbers and extension of string

  12. 12

    Extract numbers from a url's string group using regex in java

  13. 13

    java regex to extract 'only' the single digit numbers from a string

  14. 14

    round all float numbers in a string

  15. 15

    Extract all numbers in string to array

  16. 16

    Regex extract string in java

  17. 17

    Regex PHP remove all float numbers

  18. 18

    extract numbers before search string in a file

  19. 19

    Extract all characters before a period with HiveQL regex?

  20. 20

    Java Regex to extract specific words

  21. 21

    Using regex extract all digit and word numbers

  22. 22

    Php: regex get all numbers before colon

  23. 23

    Java : Extract numbers from a string

  24. 24

    Java : Extract numbers from a string

  25. 25

    Extract numbers from a string in java

  26. 26

    R regex extract number after/before string

  27. 27

    Can Regex Extract Multiple Numbers from String?

  28. 28

    Using regex to extract numbers and symbols from string

  29. 29

    extract numbers within a string using regex

HotTag

Archive