A more concise version of this Regular Expression?

eric

I am trying to match an integer or decimal number, followed by two dots .., followed by another integer or decimal number. On each side of the dots, the numbers are optional, but they must be present on one side or the other. I will be using Java for the actual implementation (String#matches()).

For example, these should match:

  • ..12
  • 12..
  • 12..24
  • 12.23..14.25

While these should not:

  • ..
  • foo
  • foo..bar
  • bazz12..12buzz
  • foo11..22
  • 11..22efg

This is the best I have been able to come up with:

(^(\d+\.?\d+)\.\.(\d+\.?\d+)?$)|(^(\d+\.?\d+)?\.\.(\d+\.?\d+)$)

I feel like it could be better. Am I wrong? Note that I have two clauses that are virtually the same with the pipe in the middle, the only difference is one matches ..12 and the other matches 12..

Edit: Thanks for all the input! I picked @anubhava because I asked for the shortest. Also thanks for pointing out the errors in my original expresion!

anubhava

You can use a lookahead to shorten the regex:

^(?=\.*\d)(?:\d+(?:\.\d+)?)?\.\.(?:\d+(?:\.\d+)?)?$

Java regex:

Pattern p = 
        Pattern.compile("^(?=\\.*\\d)(?:\\d+(?:\\.\\d+)?)?\\.\\.(?:\\d+(?:\\.\\d+)?)?$");

RegEx Demo

(?=\.*\d) is the positive lookahead that ensures there is at least one digit thus making sure we don't match just .. as a valid input.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

More concise HashMap initialization

From Dev

regular expression not containing two or more consecutive chars

From Dev

Is there a more sensible way of writing this regular expression?

From Dev

Regular Expression for matching 0 or more words with commas

From Dev

Extract version from string with Regular Expression and lookahead

From Dev

How can I make this match expression more concise?

From Dev

Python regular expression zero or more occurrences

From Dev

NSPredicate Regular Expression to match more than one segment on the same attribute

From Dev

Regular expression capturing more than expected

From Dev

Regular Expression for exactly N elements, not less, not more

From Dev

How to write a concise regular expression for all strings containing "a"s, "b"s, and "c"s but no more than 2 "b"s and 3 "c"s

From Dev

Regular Expression admits more numbers that allowed

From Dev

Version regular expression in CMake

From Dev

More concise LINQ with 'or' clause

From Dev

Regular Expression allows more than specified characters

From Dev

replace Regular Expression by a modified version of the Regular Expression result

From Dev

How can I be more specific with this regular expression?

From Dev

regular expression convert version wrong

From Dev

convert regular expression to be more specific

From Dev

Extract version from string with Regular Expression and lookahead

From Dev

Python regular expression zero or more occurrences

From Dev

Is there a more concise Coffeescript idiom for this?

From Dev

Regular expression to match version numbers

From Dev

How to write a concise regular expression for all strings containing "a"s, "b"s, and "c"s but no more than 2 "b"s and 3 "c"s

From Dev

Regular expression to match at least character and more words

From Dev

Version regular expression in CMake

From Dev

sed regular expression matching more than intended

From Dev

replace Regular Expression by a modified version of the Regular Expression result

From Dev

bash + regular expression + capture java version

Related Related

  1. 1

    More concise HashMap initialization

  2. 2

    regular expression not containing two or more consecutive chars

  3. 3

    Is there a more sensible way of writing this regular expression?

  4. 4

    Regular Expression for matching 0 or more words with commas

  5. 5

    Extract version from string with Regular Expression and lookahead

  6. 6

    How can I make this match expression more concise?

  7. 7

    Python regular expression zero or more occurrences

  8. 8

    NSPredicate Regular Expression to match more than one segment on the same attribute

  9. 9

    Regular expression capturing more than expected

  10. 10

    Regular Expression for exactly N elements, not less, not more

  11. 11

    How to write a concise regular expression for all strings containing "a"s, "b"s, and "c"s but no more than 2 "b"s and 3 "c"s

  12. 12

    Regular Expression admits more numbers that allowed

  13. 13

    Version regular expression in CMake

  14. 14

    More concise LINQ with 'or' clause

  15. 15

    Regular Expression allows more than specified characters

  16. 16

    replace Regular Expression by a modified version of the Regular Expression result

  17. 17

    How can I be more specific with this regular expression?

  18. 18

    regular expression convert version wrong

  19. 19

    convert regular expression to be more specific

  20. 20

    Extract version from string with Regular Expression and lookahead

  21. 21

    Python regular expression zero or more occurrences

  22. 22

    Is there a more concise Coffeescript idiom for this?

  23. 23

    Regular expression to match version numbers

  24. 24

    How to write a concise regular expression for all strings containing "a"s, "b"s, and "c"s but no more than 2 "b"s and 3 "c"s

  25. 25

    Regular expression to match at least character and more words

  26. 26

    Version regular expression in CMake

  27. 27

    sed regular expression matching more than intended

  28. 28

    replace Regular Expression by a modified version of the Regular Expression result

  29. 29

    bash + regular expression + capture java version

HotTag

Archive