0

I need some assistance fixing my regular expression, which should match the following conditions:

  • s and only s
  • f and only f
  • dn and only dn
  • any one or two whole numbers in front of a d (35d for example) or any number with decimals
    • ss would be invalid, dnf would be invalid, 305d would be invalid

Regular Expression:

  • ^(s|f|dn)|(\d{2}d)$ gets me started
    • sf matches, but it shouldn't.
Trenton McKinney
  • 29,033
  • 18
  • 54
  • 66
blomster
  • 606
  • 1
  • 7
  • 23
  • Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/a/2759417/3832970) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Oct 03 '19 at 13:24
  • Can you give a few more examples? – Lucas Abbade Oct 03 '19 at 13:25
  • Your question is kind of unclear. Is this what you're looking for: `(s|f|dn)|(\d{2}d)`? You may need start / end characters (`^` and `$`) depending if you're trying to match the whole string. This pattern matches: `'s'`, `'f'`, `'dn'`, or '`99d` (any 2 numbers, d). – dvo Oct 03 '19 at 13:27
  • thanks @dvo, this has given me a foundation to build off. This still allows 'sf' though, or 'ss' – blomster Oct 03 '19 at 13:37
  • @blomster play around in regex101. They both get matched individually, not together. So: `sf` is actually `s`, `f` (separate). Play with a `^` at the beginning and `$` at then end. Best of luck! – dvo Oct 03 '19 at 13:49
  • 1
    I vote to reopen. Sending an OP to a page of links is not helpful, nor is it a duplicate of this specific question about a specific regular expression. – Trenton McKinney Oct 03 '19 at 16:29

1 Answers1

0

Your | operator is gobbling up your anchors! According to this code sample, the engine is treating your first alternative as ^(s|f|dn) and your second alternative as (\d{2}d)$ - so s is a valid match, since there's nothing saying it has to be at the end of the string.

Try sticking your terms in a non-capturing group to make sure the | operator can't touch your anchors: ^(?:(s|f|dn)|(\d{2}d))$ ought to work; this sample seems to agree.

I also saw a note that you wanted any decimal number to be valid. For that, your regex is: ^(?:(s|f|dn)|(\d{2}d)|(\d+\.\d+))$ - try it here!

Nick Reed
  • 5,029
  • 4
  • 14
  • 34
  • Thanks to the community for keeping faith in me. Thanks @Nick, that was the secret sauce I needed. I also needed to match any number, so in the end this did the trick: ^(?:(s|f|dn)|(\d{2}d)|([0-9]+([.][0-9]+)?))$ – blomster Oct 07 '19 at 08:45
  • and actually, I needed to match 1 or 2 digits in front of a 'd', so (\d{2}d) became (\d{1}d|\d{2}d) I'm learning!! – blomster Oct 07 '19 at 08:53