locked
Regex -> Barre code pattern RRS feed

  • Question

  • User632428103 posted

    Hello all,

    we have an application who used an regex as this one :

    ^(?<year>[1][1-9])(?<ip>[A|X|Y|Z]\w)(?<number>\d{6})(?[3])(?<postalcode>\d{4})$</postalcode></number></ip></year>

    this regex check a format of a barre code.

    It's work fine UNTIL the end of this year because we check the date in the begining of the regex -> [1][1-9]

    Ok, I can modify the regex for to accept the next year as this -> [1-2][0-9]

    this change will accept the barre code begin by 19xxxxx OR 20xxxx OR 10xxxxx

    but i'm not really satisfied by this change because my regex MUST TO check the date 19 OR 20 

    Someone can tell me, if it's possible to do for exemple +1 -1 for to check the date ?

    ex : I would like just to accept two digits and these two digit can be only 19 OR 20

    thanks for all

    Thursday, December 5, 2019 3:40 PM

Answers

  • User475983607 posted

    The RegEx shown has syntax errors.  Why cant you use a basic OR like you are already using?

    19|20

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 5, 2019 4:04 PM
  • User-719153870 posted

    Hi jimmy69,

    jimmy69

    yes I would like to make some math if POSSIBLE ..  make something like +1 -1 ?

    I don't think the regex can do calculation, but it's easy to be done in js or any other code language depends where would you use this regex.

    For example, you can see i did the dynamic regex in JS in below demo:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script>
            function check() {
                var ty = new Date().getFullYear();//get current year here
                var y = ty.toString().substr(-2);
                var regpar = y + '|' + (parseInt(y) + 1).toString()
                var reg = new RegExp('^' + regpar + '$')
                var target = document.getElementById("ipt").value
                if (target.match(reg)) { alert(1) }
                else { alert(0) }
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <input id="ipt" type="text" value="" />
                <input type="button" value="check" onclick="check()" />
            </div>
        </form>
    </body>
    </html>

    Below is the result of this demo:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 6, 2019 9:34 AM

All replies

  • User475983607 posted

    The RegEx shown has syntax errors.  Why cant you use a basic OR like you are already using?

    19|20

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 5, 2019 4:04 PM
  • User-719153870 posted

    Hi jimmy69,

    if it's possible to do for exemple +1 -1 for to check the date ?

    Not quite understand this, are you trying to do a math in your regular expression? Based on current year and get the last&next year? In this case you should get three years 18, 19 and 20 rather than just 19 and 20.

    I would like just to accept two digits and these two digit can be only 19 OR 20

    If you just want 19 or 20, just use the basic | in regular expression like @mgebhard told.

    Best Regard,

    Yang Shen

    Friday, December 6, 2019 5:39 AM
  • User632428103 posted

    Hello all,

    thanks for your time .

    @mgebhard -> yes I can use 19|20 but next year I need to change to 20|21

    @yang Shen -> yes I would like to make some math if POSSIBLE ..  make something like +1 -1 ?

    I would like something like accept 19 20 21 22 perhpas the best solution will be to make an OR ?

    thanks for all

    Friday, December 6, 2019 9:01 AM
  • User-719153870 posted

    Hi jimmy69,

    jimmy69

    yes I would like to make some math if POSSIBLE ..  make something like +1 -1 ?

    I don't think the regex can do calculation, but it's easy to be done in js or any other code language depends where would you use this regex.

    For example, you can see i did the dynamic regex in JS in below demo:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script>
            function check() {
                var ty = new Date().getFullYear();//get current year here
                var y = ty.toString().substr(-2);
                var regpar = y + '|' + (parseInt(y) + 1).toString()
                var reg = new RegExp('^' + regpar + '$')
                var target = document.getElementById("ipt").value
                if (target.match(reg)) { alert(1) }
                else { alert(0) }
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <input id="ipt" type="text" value="" />
                <input type="button" value="check" onclick="check()" />
            </div>
        </form>
    </body>
    </html>

    Below is the result of this demo:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 6, 2019 9:34 AM
  • User632428103 posted

    Hello all,

    thanks for your time you spend here ..

    great to find any help and advice

    Monday, December 9, 2019 8:17 AM