# Request assistance to write script using Regex

**URL:** <https://forum.ansible.com/t/request-assistance-to-write-script-using-regex/35500>\
**Category:** Ansible Project\
**Created:** [July 13, 2021, 5:27pm UTC](https://forum.ansible.com/t/request-assistance-to-write-script-using-regex/35500 "2021-07-13T17:27:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vikram\_S](https://avatars.discourse-cdn.com/v4/letter/v/90db22/32.png) [@Vikram\_S](https://forum.ansible.com/u/Vikram_S)\
**Post date:** [July 13, 2021, 5:27pm UTC](https://forum.ansible.com/t/request-assistance-to-write-script-using-regex/35500/1 "2021-07-13T17:27:04Z")

</div>

Hi Team,

From the below output, i need to extract only the IP 10.1.1.10 (it is ok if the entire line starting from asterix is extracted).

I wrote below script and it seemed to work when i tested in [regex101.com](http://regex101.com/) but failing when i used it in playbook. What am i doing wrong? How can it be improved?

**SWITCH COMMAND:**

CORE-SWITCH#sh ip route 0.0.0.0  
Routing entry for [0.0.0.0/0](http://0.0.0.0/0), supernet  
Known via “static”, distance 1, metric 0, candidate default path  
Redistributing via eigrp 500  
Advertised by eigrp 500  
Routing Descriptor Blocks:

- 10.1.1.10  
Route metric is 0, traffic share count is 1

**SCRIPT:**

tasks:

- name: GATHER DEFAULT ROUTE INFO  
ios\_command:  
commands: “show ip route 0.0.0.0”

register: default\_route

- debug:  
msg: “{{ default\_route.stdout[0] | regex\_findall(‘\* (.\*)’) }}”

**OUTPUT:**

ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each:  
JSON: Expecting value: line 1 column 1 (char 0)

Syntax Error while loading YAML.  
found unknown escape character

The error appears to be in ‘/root/sample12.yaml’: line 19, column 59, but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- debug:  
msg: “{{ default\_route.stdout[0] | regex\_findall(‘\* (.\*)’) }}”  
^ here  
We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value. For instance:

with\_items:

- {{ foo }}

Should be written as:

with\_items:

- “{{ foo }}”

Thanks,  
Vikram

---

<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:** [July 13, 2021, 5:36pm UTC](https://forum.ansible.com/t/request-assistance-to-write-script-using-regex/35500/2 "2021-07-13T17:36:07Z")

</div>

Well, it's says it right there:
