• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RegEx to match a number

 
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ! I try very hard to find a regEx which match to a special number but I really can't Can you help me ?
The number wich I need to match have different "letter".
First, the beginning have to be a word boundary and the end a non-word boundary (\b, \B...)
It can start with this letter "~": Only, with or without
After, with a "-": With or without
Then, there is a number like that: 0;4;1.4;902.67892 => [0-9]+(\.[0-9]*)?
And one last little thing: numbers like x. and .x are correct !

Some Exemples of numbers because I explain really bad:
~
3
~3
~-3
~3.44
.44
3.
~-3.

That's it ! I hope you understand, thank you in advance for your help
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Have you consulted the java.util.regex.Pattern class?

Personally I will treat the ~ and - and . as is, then the digit \d in between. The {0,1} just say 0 or 1 time.

~{0,1}
-{0,1}
.{0,1}
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I presume you are using ~ for negation. What sort of a number would ~ be?
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought that just meant "approximately".
 
Menoux Mathieu
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answers !
Campbell Ritchie and Dave Tolls, in fact, my numbers are coordinates: ~ mean relative coordinate and - is effectively for negative numbers ;)
K. Tsang, here is my progress, your answer can't work
For the moment I ignore the word boundary.
So it can be just ~:
Or a number:
Maybe it's a decimal one:
It can be negative:
And Relative:
But here are the problem: the number "x" can also be like that: .x
If I do that: numbers will be able to be skipped and so -, . or ~- will be correct numbers while there aren't.
Oh my god it's really complicated...
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you searched for regular expressions for decimal numbers? You can find a ready‑made regex easily enough; if you go through the documentation for Scanner and Formatter, you will find regexes for numbers. You can then add plain simple - and ~ to them, or as alternatives. Remember that − is a meta‑character, so you should change it to "\\-". You can check here, but I think that ~ is not a meta‑character.
 
Menoux Mathieu
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Have you searched for regular expressions for decimal numbers? You can find a ready‑made regex easily enough; if you go through the documentation for Scanner and Formatter, you will find regexes for numbers. You can then add plain simple - and ~ to them, or as alternatives. Remember that − is a meta‑character, so you should change it to "\\-". You can check here, but I think that ~ is not a meta‑character.


But the RegEx for decimal number is that, no ?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Menoux Mathieu wrote:
But the RegEx for decimal number is that, no ?



Yes. That is the regex for a decimal number where the left of the decimal point is mandatory, and to the right of the decimal point (along with the decimal point) are optional. Can you modify this regex a little bit to deal with the other case? ... where to the right of the decimal is mandatory, and the left is optional?

Henry
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Besides using the [0-9] for digits, you can opt to use \d

I actually managed to come up with something like ~{0,1}-{0,1}\d{0,}.{0,1}\d{0,} but of course that may not fulfill "all" cases and it might not be the best representation of regex.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you use the {} notation in place of the simpler +, * and ?
 
Menoux Mathieu
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry Wong, I don't find any way to do that without an optional whole number :/ So the only way is to do a second RegEx for this:
And it works ! So here are the solution to my question

I just have to made some test to have the right word boundary:
and here it is ! Thanks everyone for your help !
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Menoux Mathieu wrote:Henry Wong, I don't find any way to do that without an optional whole number :/ So the only way is to do a second RegEx for this:



That will work... In fact, it is better than what I was thinking, as the two cases are merged together by the alternation operator.

Menoux Mathieu wrote:
And it works ! So here are the solution to my question



Thanks for reporting on the solution. Have a cow.

Henry
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sometimes, it's best not to write a single, massive regex that covers every possible situation, but instead write several smaller ones - just like writing methods in a program.

I can't say if that would work in this specific situation, as I haven't spent much time deciphering your requirements, but it is something to consider.
 
Menoux Mathieu
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:Sometimes, it's best not to write a single, massive regex that covers every possible situation, but instead write several smaller ones - just like writing methods in a program.

I can't say if that would work in this specific situation, as I haven't spent much time deciphering your requirements, but it is something to consider.


I agree with you, that's what I do with others regEx things But for my program I must collect the whole number and not just a part.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred means to match parts of the text, and put the regexes together to match the whole of your number.
 
Menoux Mathieu
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Fred means to match parts of the text, and put the regexes together to match the whole of your number.

I use alternance "|" instead of different matches, I think it's also clear and more simpler !
 
Forget Steve. Look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic