Click here to Skip to main content
15,504,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been pounding my head trying to figure out a regex for the following scenario.

I need the following to be matched:

X11
X.25
X12.
X-21.25

I don't want this matched:
X

I have come close with 2 options but both have issues. The 1st expression listed below won't match the missing decimal point on the first test in my list. If I add a question mark to make the decimal optional it fixes my initial problem but then it matches the standalone X without a number which I don't want.

What I have tried:

([X]-?[0-9]*\.)[0-9]*

([X]-?[0-9]*\.?)[0-9]*
Posted
Updated 23-Sep-22 14:24pm

Try this:
^X-?((\d+)|(\d+\.\d*)|(\.\d+))$

^X-?
      Beginning of line or string
      X-, zero or one repetitions
  [1]: A numbered capture group. [(\d+)|(\d+\.\d*)|(\.\d+)]
      Select from 3 alternatives
          [2]: A numbered capture group. [\d+]
              Any digit, one or more repetitions
          [3]: A numbered capture group. [\d+\.\d*]
              \d+\.\d*
                  Any digit, one or more repetitions
                  Literal .
                  Any digit, any number of repetitions
          [4]: A numbered capture group. [\.\d+]
              \.\d+
                  Literal .
                  Any digit, one or more repetitions
  End of line or string
 
Share this answer
 
Comments
theskiguy 26-Sep-22 7:56am    
Thank you for the quick response and the explanation on how the string was created
OriginalGriff 26-Sep-22 8:37am    
You're welcome!
Quote:
([X]-?[0-9]*\.)[0-9]*

As many parts are optionnal, you need a RegEx a little more complicated.
[X]-?(\d+\.*[0-9]*|.[0-9]*)


Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 
Comments
theskiguy 26-Sep-22 7:57am    
Thank you. Your solution works as well. Also, I appreciate the links for continued learning.

Edit: After continued testing, I also determined I can't allow "X-" alone without a number. Since I didn't include this stipulation in my original question, your solution still works for the original question. If someone else needs an expression to reject "X-" alone without a number as well as the other stipulations listed, Original Griff's solution works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900