Regex to find a float, maybe a REALLY simple question

I have never used a regex before, but this function requires java (shown here: How to set the Edittext view to allow only two numeric values ​​and two decimal values, for example ##. # # )

I basically just need to get a float from it with a text box, it should be simple. I used the tool, and he said this should work:

String re1="([+-]?\\d*\\.\\d+)(?![-+0-9\\.])"; 

But it does not seem to work, it does not allow me to put anything in the text box.

What is the right way to do this? Thanks

+6
source share
4 answers

Try the following:

 String re1="^([+-]?\\d*\\.?\\d*)$"; 
+8
source

The correct way for this problem is not to use a regex, but simply use:

 try { Float.parseFloat(string) return true; } catch (NumberFormatException ex) { return false; } 

Works fine, this is the same code that is later used to parse the float and therefore the error (or if we don't have a much more serious problem).

+5
source

This is the correct way:

 String floatRegexp="^([+-]?(\\d+\\.)?\\d+)$"; 

or if you look also in the middle of another text:

 String floatRegexp="([+-]?(\\d+\\.)?\\d+)"; 

and if you are not looking for the minus / plus sign:

 String floatRegexp="^(\\d+\\.)?\\d+$"; 
+3
source

This answer is from Johan SjΓΆberg fooobar.com/questions/893110 / ...

You can try matching numbers using regex

 \\d+\\.\\d+ 

It might look something like this:

 Pattern p = Pattern.compile("\\d+\\.\\d+"); Matcher m = p.matcher("Sstring>26.0.[2.3.2.3D] .352.(f) 1)"503B"(\1.67>>Sstring"); while (m.find()) { System.out.println(Float.parseFloat(m.group())); } 

conclusion:

 26.0 2.3 2.4 1.67 

the [2.3.2.3D] string is split into two separate floating-point numbers

0
source

Source: https://habr.com/ru/post/893108/


All Articles