# Assert using Regex

**URL:** <https://forum.ansible.com/t/assert-using-regex/35170>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [May 14, 2021, 3:23pm UTC](https://forum.ansible.com/t/assert-using-regex/35170 "2021-05-14T15:23:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![amitchettri](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/amitchettri/32/404_2.png) [@amitchettri](https://forum.ansible.com/u/amitchettri)\
**Post date:** [May 14, 2021, 3:23pm UTC](https://forum.ansible.com/t/assert-using-regex/35170/1 "2021-05-14T15:23:26Z")

</div>

Hello,

I am able to do the assert using basic string check. But i am looking for a way to do using regex.

example.  
i have register: result with below value

**“stdout\_lines”: [**  
**“microsoft (R) Windows Script Host Version 5.812”,**  
**“Copyright (C) Microsoft Corporation. All rights reserved.”,**  
**“Software licensing service version: 10.0.17763.107”,**  
**“Name: Windows(R), ServerStandard edition”,**  
**“Description: Windows(R) Operating System, VOLUME\_MAK channel”,**  
**“Activation ID: xxxxxxxx-e4b1-483b-81e8-xxxxxxx”,**  
**“Application ID: xxxxxxx-d682-4d71-983e-xxxxxxx”,**  
**“Extended PID: 03612-xxxxxx-071-xxxxxx-03-1033-1xxx7763.0000-xxxxx”,**  
**“Product Key Channel: Volume:MAK”,**  
**“Installation ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxx”,**  
**“Use License URL: [https://activation-ssssssssssssssss?configextension=Retail](https://activation-ssssssssssssssss?configextension=Retail)”,**  
**“Validation URL: [https://validation-v2.sls.microsoft.com/SLWGA/slwga.asmx](https://validation-v2.sls.microsoft.com/SLWGA/slwga.asmx)”,**  
**“Partial Product Key: djkahdjka”,**  
**“License Status: Licensed”,**  
**“Remaining Windows rearm count: 1000”,**  
**“Remaining SKU rearm count: 1001”,**  
**“Trusted time: 5/13/2021 2:51:47 PM”**  
**]**

using the code as below i am able to do the assert

- assert:  
that:
- “‘License Status: Licensed’ in result.stdout\_lines”
- “‘Product Key Channel: Volume:MAK’”
- “’ **Remaining Windows rearm count: 1000** ’ in result.stdout\_lines”  
success\_msg: “activated”  
fail\_msg: “not activated”

My problem is that i " **Remaining Windows rearm count"** can be between 1000-2000, do we have way to get this check as right now its static to **1000**

**regards**

**Amit**

---

<div class="post-metadata">

**Author:** ![The\_Vandyy\_Vines](https://avatars.discourse-cdn.com/v4/letter/t/7993a0/32.png) [@The\_Vandyy\_Vines](https://forum.ansible.com/u/The_Vandyy_Vines)\
**Post date:** [May 14, 2021, 3:31pm UTC](https://forum.ansible.com/t/assert-using-regex/35170/2 "2021-05-14T15:31:28Z")

</div>

Can uou use \>= with 1000

---

<div class="post-metadata">

**Author:** ![amitchettri](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/amitchettri/32/404_2.png) [@amitchettri](https://forum.ansible.com/u/amitchettri)\
**Post date:** [May 14, 2021, 3:44pm UTC](https://forum.ansible.com/t/assert-using-regex/35170/3 "2021-05-14T15:44:20Z")

</div>

that may not be possible as all of this are string under **register.stdout\_lines**

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 14, 2021, 3:47pm UTC](https://forum.ansible.com/t/assert-using-regex/35170/4 "2021-05-14T15:47:35Z")

</div>

Adding this to the list of assertions will succeed for number between  
1000 and 1999:

- "result.stdout\_lines|regex\_search('Remaining Windows rearm count: 1\\d{3}')"

Tweak the regex some more to include 2000 itself as well.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 14, 2021, 4:09pm UTC](https://forum.ansible.com/t/assert-using-regex/35170/5 "2021-05-14T16:09:01Z")

</div>

You can also first extract that number so you can use the standard \>,  
\< etc tests, for example:

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;remaining\_windows\_rearm\_count: "{{  
result.stdout\_lines|regex\_replace('.\*Remaining Windows rearm count:  
(\\d+).\*', '\\1' ) }}"

&nbsp;&nbsp;&nbsp;&nbsp;- assert:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "'License Status: Licensed' in result.stdout\_lines"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "'Product Key Channel: Volume:MAK'"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 2000 \>= remaining\_windows\_rearm\_count|int \> 1000  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;success\_msg: "activated"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fail\_msg: "not activated"

---

<div class="post-metadata">

**Author:** ![amitchettri](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/amitchettri/32/404_2.png) [@amitchettri](https://forum.ansible.com/u/amitchettri)\
**Post date:** [May 18, 2021, 11:00pm UTC](https://forum.ansible.com/t/assert-using-regex/35170/6 "2021-05-18T23:00:39Z")

</div>

Hi Dick,

Sorry for reverting for back late. Thanks a lot for the above sample. this really help me.

Regards  
Amit
