RegEx pattern not asserting true










2














I am trying to compare the below string to a regular expression pattern in my java project:



2018-11-12 12:02:04.075



I've passed the above string into an online regular expression generator to generate the following pattern:



((?:2|1)d3(?:-|/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))



Below is my ASSERT statement, but when I run this test I get an assertion error:



assertTrue(rs.getString(this.columnName).matches("((?:2|1)\d3(?:-|\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))"));


What changes do I need to make to get this test to pass? Is it a problem with my regex?










share|improve this question

















  • 1




    I don't know the defaults for Java's matches method but are you sure it isn't trying to match the whole string?
    – Lieven Keersmaekers
    Nov 12 at 12:27






  • 1




    This seems a lot more complicated than just using a DateTimeFormatter. For that matter, why not define your database column as a datetime or timestamp column?
    – VGR
    Nov 12 at 15:50















2














I am trying to compare the below string to a regular expression pattern in my java project:



2018-11-12 12:02:04.075



I've passed the above string into an online regular expression generator to generate the following pattern:



((?:2|1)d3(?:-|/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))



Below is my ASSERT statement, but when I run this test I get an assertion error:



assertTrue(rs.getString(this.columnName).matches("((?:2|1)\d3(?:-|\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))"));


What changes do I need to make to get this test to pass? Is it a problem with my regex?










share|improve this question

















  • 1




    I don't know the defaults for Java's matches method but are you sure it isn't trying to match the whole string?
    – Lieven Keersmaekers
    Nov 12 at 12:27






  • 1




    This seems a lot more complicated than just using a DateTimeFormatter. For that matter, why not define your database column as a datetime or timestamp column?
    – VGR
    Nov 12 at 15:50













2












2








2







I am trying to compare the below string to a regular expression pattern in my java project:



2018-11-12 12:02:04.075



I've passed the above string into an online regular expression generator to generate the following pattern:



((?:2|1)d3(?:-|/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))



Below is my ASSERT statement, but when I run this test I get an assertion error:



assertTrue(rs.getString(this.columnName).matches("((?:2|1)\d3(?:-|\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))"));


What changes do I need to make to get this test to pass? Is it a problem with my regex?










share|improve this question













I am trying to compare the below string to a regular expression pattern in my java project:



2018-11-12 12:02:04.075



I've passed the above string into an online regular expression generator to generate the following pattern:



((?:2|1)d3(?:-|/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))



Below is my ASSERT statement, but when I run this test I get an assertion error:



assertTrue(rs.getString(this.columnName).matches("((?:2|1)\d3(?:-|\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))"));


What changes do I need to make to get this test to pass? Is it a problem with my regex?







java regex assert






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 12:13









user9847788

132




132







  • 1




    I don't know the defaults for Java's matches method but are you sure it isn't trying to match the whole string?
    – Lieven Keersmaekers
    Nov 12 at 12:27






  • 1




    This seems a lot more complicated than just using a DateTimeFormatter. For that matter, why not define your database column as a datetime or timestamp column?
    – VGR
    Nov 12 at 15:50












  • 1




    I don't know the defaults for Java's matches method but are you sure it isn't trying to match the whole string?
    – Lieven Keersmaekers
    Nov 12 at 12:27






  • 1




    This seems a lot more complicated than just using a DateTimeFormatter. For that matter, why not define your database column as a datetime or timestamp column?
    – VGR
    Nov 12 at 15:50







1




1




I don't know the defaults for Java's matches method but are you sure it isn't trying to match the whole string?
– Lieven Keersmaekers
Nov 12 at 12:27




I don't know the defaults for Java's matches method but are you sure it isn't trying to match the whole string?
– Lieven Keersmaekers
Nov 12 at 12:27




1




1




This seems a lot more complicated than just using a DateTimeFormatter. For that matter, why not define your database column as a datetime or timestamp column?
– VGR
Nov 12 at 15:50




This seems a lot more complicated than just using a DateTimeFormatter. For that matter, why not define your database column as a datetime or timestamp column?
– VGR
Nov 12 at 15:50












3 Answers
3






active

oldest

votes


















0














Try using one of the following regexes:



((?:2|1)d3(?:-|/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9].(?:[0-9][0-9][0-9])))


or



((?:2|1)d3(?:-|/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]).(d3))


or



(d4)-(d2)-(d2) (d2):(d2):(d2).(d3)





share|improve this answer






















  • Thanks, the first one worked for me. May I ask what the difference is between these 3 options. And is there a preferred one? I would think the shorter one would be preferrable?
    – user9847788
    Nov 12 at 12:32










  • first one will match date-time with different separators
    – Aeiman Bakeer
    Nov 12 at 13:00


















2














You are missing the miliseconds part. Try this:



((?:2|1)\d3(?:-|\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9])\.\d3)





share|improve this answer




























    0














    Try this



    [12]\d3-(0[1-9]|1[12])-(0[1-9]|1[0-9]|2[0-9]|3[01])\s([01][0-9]|2[0-4]):[0-5][0-9]:[0-5][0-9]\.\d3





    share|improve this answer






















    • This answer would be improved by explaining why the particular pattern meets the requirements. Also, by not checking the first digit, this will match 9999-46-98 76:62:99.000, so I think it is too broad of a match.
      – KevinO
      Nov 12 at 15:57










    • @KevinO the question was to match a specific date-time format, which used "-" as separator, and did not specify year range, though I've limited it upon your suggestion
      – Aeiman Bakeer
      Nov 13 at 17:05










    • I think this is improved. While the specific text did not specify a year range, the OP's attempt was to try to limit the possible validity, so it seems appropriate to keep close to what the OP at least attempted.
      – KevinO
      Nov 13 at 17:08










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261980%2fregex-pattern-not-asserting-true%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Try using one of the following regexes:



    ((?:2|1)d3(?:-|/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9].(?:[0-9][0-9][0-9])))


    or



    ((?:2|1)d3(?:-|/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]).(d3))


    or



    (d4)-(d2)-(d2) (d2):(d2):(d2).(d3)





    share|improve this answer






















    • Thanks, the first one worked for me. May I ask what the difference is between these 3 options. And is there a preferred one? I would think the shorter one would be preferrable?
      – user9847788
      Nov 12 at 12:32










    • first one will match date-time with different separators
      – Aeiman Bakeer
      Nov 12 at 13:00















    0














    Try using one of the following regexes:



    ((?:2|1)d3(?:-|/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9].(?:[0-9][0-9][0-9])))


    or



    ((?:2|1)d3(?:-|/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]).(d3))


    or



    (d4)-(d2)-(d2) (d2):(d2):(d2).(d3)





    share|improve this answer






















    • Thanks, the first one worked for me. May I ask what the difference is between these 3 options. And is there a preferred one? I would think the shorter one would be preferrable?
      – user9847788
      Nov 12 at 12:32










    • first one will match date-time with different separators
      – Aeiman Bakeer
      Nov 12 at 13:00













    0












    0








    0






    Try using one of the following regexes:



    ((?:2|1)d3(?:-|/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9].(?:[0-9][0-9][0-9])))


    or



    ((?:2|1)d3(?:-|/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]).(d3))


    or



    (d4)-(d2)-(d2) (d2):(d2):(d2).(d3)





    share|improve this answer














    Try using one of the following regexes:



    ((?:2|1)d3(?:-|/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9].(?:[0-9][0-9][0-9])))


    or



    ((?:2|1)d3(?:-|/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]).(d3))


    or



    (d4)-(d2)-(d2) (d2):(d2):(d2).(d3)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 at 14:24









    Sven Hakvoort

    2,0271519




    2,0271519










    answered Nov 12 at 12:26









    vignesh

    161




    161











    • Thanks, the first one worked for me. May I ask what the difference is between these 3 options. And is there a preferred one? I would think the shorter one would be preferrable?
      – user9847788
      Nov 12 at 12:32










    • first one will match date-time with different separators
      – Aeiman Bakeer
      Nov 12 at 13:00
















    • Thanks, the first one worked for me. May I ask what the difference is between these 3 options. And is there a preferred one? I would think the shorter one would be preferrable?
      – user9847788
      Nov 12 at 12:32










    • first one will match date-time with different separators
      – Aeiman Bakeer
      Nov 12 at 13:00















    Thanks, the first one worked for me. May I ask what the difference is between these 3 options. And is there a preferred one? I would think the shorter one would be preferrable?
    – user9847788
    Nov 12 at 12:32




    Thanks, the first one worked for me. May I ask what the difference is between these 3 options. And is there a preferred one? I would think the shorter one would be preferrable?
    – user9847788
    Nov 12 at 12:32












    first one will match date-time with different separators
    – Aeiman Bakeer
    Nov 12 at 13:00




    first one will match date-time with different separators
    – Aeiman Bakeer
    Nov 12 at 13:00













    2














    You are missing the miliseconds part. Try this:



    ((?:2|1)\d3(?:-|\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9])\.\d3)





    share|improve this answer

























      2














      You are missing the miliseconds part. Try this:



      ((?:2|1)\d3(?:-|\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9])\.\d3)





      share|improve this answer























        2












        2








        2






        You are missing the miliseconds part. Try this:



        ((?:2|1)\d3(?:-|\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9])\.\d3)





        share|improve this answer












        You are missing the miliseconds part. Try this:



        ((?:2|1)\d3(?:-|\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9])\.\d3)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 12:27









        Evgeni Enchev

        2015




        2015





















            0














            Try this



            [12]\d3-(0[1-9]|1[12])-(0[1-9]|1[0-9]|2[0-9]|3[01])\s([01][0-9]|2[0-4]):[0-5][0-9]:[0-5][0-9]\.\d3





            share|improve this answer






















            • This answer would be improved by explaining why the particular pattern meets the requirements. Also, by not checking the first digit, this will match 9999-46-98 76:62:99.000, so I think it is too broad of a match.
              – KevinO
              Nov 12 at 15:57










            • @KevinO the question was to match a specific date-time format, which used "-" as separator, and did not specify year range, though I've limited it upon your suggestion
              – Aeiman Bakeer
              Nov 13 at 17:05










            • I think this is improved. While the specific text did not specify a year range, the OP's attempt was to try to limit the possible validity, so it seems appropriate to keep close to what the OP at least attempted.
              – KevinO
              Nov 13 at 17:08















            0














            Try this



            [12]\d3-(0[1-9]|1[12])-(0[1-9]|1[0-9]|2[0-9]|3[01])\s([01][0-9]|2[0-4]):[0-5][0-9]:[0-5][0-9]\.\d3





            share|improve this answer






















            • This answer would be improved by explaining why the particular pattern meets the requirements. Also, by not checking the first digit, this will match 9999-46-98 76:62:99.000, so I think it is too broad of a match.
              – KevinO
              Nov 12 at 15:57










            • @KevinO the question was to match a specific date-time format, which used "-" as separator, and did not specify year range, though I've limited it upon your suggestion
              – Aeiman Bakeer
              Nov 13 at 17:05










            • I think this is improved. While the specific text did not specify a year range, the OP's attempt was to try to limit the possible validity, so it seems appropriate to keep close to what the OP at least attempted.
              – KevinO
              Nov 13 at 17:08













            0












            0








            0






            Try this



            [12]\d3-(0[1-9]|1[12])-(0[1-9]|1[0-9]|2[0-9]|3[01])\s([01][0-9]|2[0-4]):[0-5][0-9]:[0-5][0-9]\.\d3





            share|improve this answer














            Try this



            [12]\d3-(0[1-9]|1[12])-(0[1-9]|1[0-9]|2[0-9]|3[01])\s([01][0-9]|2[0-4]):[0-5][0-9]:[0-5][0-9]\.\d3






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 at 17:36

























            answered Nov 12 at 13:03









            Aeiman Bakeer

            37037




            37037











            • This answer would be improved by explaining why the particular pattern meets the requirements. Also, by not checking the first digit, this will match 9999-46-98 76:62:99.000, so I think it is too broad of a match.
              – KevinO
              Nov 12 at 15:57










            • @KevinO the question was to match a specific date-time format, which used "-" as separator, and did not specify year range, though I've limited it upon your suggestion
              – Aeiman Bakeer
              Nov 13 at 17:05










            • I think this is improved. While the specific text did not specify a year range, the OP's attempt was to try to limit the possible validity, so it seems appropriate to keep close to what the OP at least attempted.
              – KevinO
              Nov 13 at 17:08
















            • This answer would be improved by explaining why the particular pattern meets the requirements. Also, by not checking the first digit, this will match 9999-46-98 76:62:99.000, so I think it is too broad of a match.
              – KevinO
              Nov 12 at 15:57










            • @KevinO the question was to match a specific date-time format, which used "-" as separator, and did not specify year range, though I've limited it upon your suggestion
              – Aeiman Bakeer
              Nov 13 at 17:05










            • I think this is improved. While the specific text did not specify a year range, the OP's attempt was to try to limit the possible validity, so it seems appropriate to keep close to what the OP at least attempted.
              – KevinO
              Nov 13 at 17:08















            This answer would be improved by explaining why the particular pattern meets the requirements. Also, by not checking the first digit, this will match 9999-46-98 76:62:99.000, so I think it is too broad of a match.
            – KevinO
            Nov 12 at 15:57




            This answer would be improved by explaining why the particular pattern meets the requirements. Also, by not checking the first digit, this will match 9999-46-98 76:62:99.000, so I think it is too broad of a match.
            – KevinO
            Nov 12 at 15:57












            @KevinO the question was to match a specific date-time format, which used "-" as separator, and did not specify year range, though I've limited it upon your suggestion
            – Aeiman Bakeer
            Nov 13 at 17:05




            @KevinO the question was to match a specific date-time format, which used "-" as separator, and did not specify year range, though I've limited it upon your suggestion
            – Aeiman Bakeer
            Nov 13 at 17:05












            I think this is improved. While the specific text did not specify a year range, the OP's attempt was to try to limit the possible validity, so it seems appropriate to keep close to what the OP at least attempted.
            – KevinO
            Nov 13 at 17:08




            I think this is improved. While the specific text did not specify a year range, the OP's attempt was to try to limit the possible validity, so it seems appropriate to keep close to what the OP at least attempted.
            – KevinO
            Nov 13 at 17:08

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261980%2fregex-pattern-not-asserting-true%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3