Replace all token not preserving leading zeroes

I'm trying to pull the digits out of a user's CN, and then prepend a fixed string. Does anyone know why this behavior is occurring and/or how to preserve the actual capture group?

[07/23/17 16:28:23.417]:UTILITY ST: Action: do-set-local-variable("username",scope="policy",token-replace-all(". (\d ). ","xdor\1",token-attr("cn"))).
[07/23/17 16:28:23.418]:UTILITY ST: arg-string(token-replace-all(". (\d ). ","xdor\1",token-attr("cn")))
[07/23/17 16:28:23.419]:UTILITY ST: token-replace-all(". (\d ). ","xdor\1",token-attr("cn"))
[07/23/17 16:28:23.420]:UTILITY ST: token-replace-all(". (\d ). ","xdor\1",token-attr("cn"))
[07/23/17 16:28:23.421]:UTILITY ST: token-attr("cn")
[07/23/17 16:28:23.421]:UTILITY ST: Token Value: "xdor0001".
[07/23/17 16:28:23.422]:UTILITY ST: Arg Value: "xdor0001".
[07/23/17 16:28:23.422]:UTILITY ST: Token Value: "xdor1".
[07/23/17 16:28:23.423]:UTILITY ST: Arg Value: "xdor1".
Parents
  • On 07/23/2017 02:44 PM, kbuley wrote:
    >
    > I'm trying to pull the digits out of a user's CN, and then prepend a
    > fixed string. Does anyone know why this behavior is occurring and/or how
    > to preserve the actual capture group?
    >
    > [07/23/17 16:28:23.417]:UTILITY ST: Action:
    > do-set-local-variable("username",scope="policy",token-replace-all(". (\d ). ","xdor\1",token-attr("cn"))).


    This is working as you defined it, but may not be working as expected
    because of a concept known as "greediness" in regular expressions. Simply
    put, tokens are greedy, meaning they try to match as much as possible, and
    only give back when they must. . matches everything, and of course is
    greedy by default, so you should use t sparingly if possible.

    Alternatively, you can tell a quantifier (like * or ) to NOT be greedy by
    default by appending it with a '?', like so:


    .. ?(\d ).


    This basically means match a little as possible, instead of as much as
    possible. Alternatively, and probably better, match non-digits explicitly
    with \D (capital D is the inverse of d) or, to be really specific about
    what you are matching, replace non-digits explicitly since \d matches some
    non-digits too, like the decimal point:


    [^0-9]


    If you are using replace-all, I would think you could just match on the
    above, replace it with nothing, and then explicitly stick some text
    before/after without using the regular expression and it would be easier
    to understand and modify by whomever in the future.

    --
    Good luck.

    If you find this post helpful and are logged into the web interface,
    show your appreciation and click on the star below.

    If you want to send me a private message, please let me know in the
    forum as I do not use the web interface often.
  • Man, it's been a rough weekend. The replacement being greedy had crossed my mind, but some reason I thought * was greedy while was lazy (like I said, rough weekend).

    <do-set-local-variable name="username" scope="policy">
    <arg-string>
    <token-text xml:space="preserve">xdor</token-text>
    <token-replace-all regex="[^0-9]" replace-with="">
    <token-attr name="cn"/>
    </token-replace-all>
    </arg-string>
    </do-set-local-variable>

    Appears to give the results I'm expecting. Muchly appreciated!


    ab;2462408 wrote:
    On 07/23/2017 02:44 PM, kbuley wrote:
    >
    > I'm trying to pull the digits out of a user's CN, and then prepend a
    > fixed string. Does anyone know why this behavior is occurring and/or how
    > to preserve the actual capture group?
    >
    > [07/23/17 16:28:23.417]:UTILITY ST: Action:
    > do-set-local-variable("username",scope="policy",token-replace-all(". (\d ). ","xdor\1",token-attr("cn"))).


    This is working as you defined it, but may not be working as expected
    because of a concept known as "greediness" in regular expressions. Simply
    put, tokens are greedy, meaning they try to match as much as possible, and
    only give back when they must. . matches everything, and of course is
    greedy by default, so you should use t sparingly if possible.

    Alternatively, you can tell a quantifier (like * or ) to NOT be greedy by
    default by appending it with a '?', like so:


    .. ?(\d ).


    This basically means match a little as possible, instead of as much as
    possible. Alternatively, and probably better, match non-digits explicitly
    with \D (capital D is the inverse of d) or, to be really specific about
    what you are matching, replace non-digits explicitly since \d matches some
    non-digits too, like the decimal point:


    [^0-9]


    If you are using replace-all, I would think you could just match on the
    above, replace it with nothing, and then explicitly stick some text
    before/after without using the regular expression and it would be easier
    to understand and modify by whomever in the future.

    --
    Good luck.

    If you find this post helpful and are logged into the web interface,
    show your appreciation and click on the star below.

    If you want to send me a private message, please let me know in the
    forum as I do not use the web interface often.
Reply
  • Man, it's been a rough weekend. The replacement being greedy had crossed my mind, but some reason I thought * was greedy while was lazy (like I said, rough weekend).

    <do-set-local-variable name="username" scope="policy">
    <arg-string>
    <token-text xml:space="preserve">xdor</token-text>
    <token-replace-all regex="[^0-9]" replace-with="">
    <token-attr name="cn"/>
    </token-replace-all>
    </arg-string>
    </do-set-local-variable>

    Appears to give the results I'm expecting. Muchly appreciated!


    ab;2462408 wrote:
    On 07/23/2017 02:44 PM, kbuley wrote:
    >
    > I'm trying to pull the digits out of a user's CN, and then prepend a
    > fixed string. Does anyone know why this behavior is occurring and/or how
    > to preserve the actual capture group?
    >
    > [07/23/17 16:28:23.417]:UTILITY ST: Action:
    > do-set-local-variable("username",scope="policy",token-replace-all(". (\d ). ","xdor\1",token-attr("cn"))).


    This is working as you defined it, but may not be working as expected
    because of a concept known as "greediness" in regular expressions. Simply
    put, tokens are greedy, meaning they try to match as much as possible, and
    only give back when they must. . matches everything, and of course is
    greedy by default, so you should use t sparingly if possible.

    Alternatively, you can tell a quantifier (like * or ) to NOT be greedy by
    default by appending it with a '?', like so:


    .. ?(\d ).


    This basically means match a little as possible, instead of as much as
    possible. Alternatively, and probably better, match non-digits explicitly
    with \D (capital D is the inverse of d) or, to be really specific about
    what you are matching, replace non-digits explicitly since \d matches some
    non-digits too, like the decimal point:


    [^0-9]


    If you are using replace-all, I would think you could just match on the
    above, replace it with nothing, and then explicitly stick some text
    before/after without using the regular expression and it would be easier
    to understand and modify by whomever in the future.

    --
    Good luck.

    If you find this post helpful and are logged into the web interface,
    show your appreciation and click on the star below.

    If you want to send me a private message, please let me know in the
    forum as I do not use the web interface often.
Children
No Data