Absent Member.
Absent Member.
368 views

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".
Labels (1)
0 Likes
3 Replies
Highlighted
Knowledge Partner
Knowledge Partner

Re: Replace all token not preserving leading zeroes

Try to do the inverse and use a regex that matches anything except a digit
and replaces each match with an empty string.
For example a regex of:

[^\d]+

Then take that output and pretend it with your string. (Replace first)
Alex McHugh - Knowledge Partner - Stavanger, Norway
Who are the Knowledge Partners
If you appreciate my comments, please click the Like button.
If I have resolved your issue, please click the Accept as Solution button.
0 Likes
Highlighted
Knowledge Partner
Knowledge Partner

Re: Replace all token not preserving leading zeroes

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.
0 Likes
Highlighted
Absent Member.
Absent Member.

Re: Replace all token not preserving leading zeroes

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.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.