# Search upto first occurence of the pattern

**URL:** <https://forum.ansible.com/t/search-upto-first-occurence-of-the-pattern/32496>\
**Category:** Ansible Project\
**Created:** [March 18, 2020, 3:20pm UTC](https://forum.ansible.com/t/search-upto-first-occurence-of-the-pattern/32496 "2020-03-18T15:20:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sivaraman\_P](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sivaraman\_P](https://forum.ansible.com/u/Sivaraman_P)\
**Post date:** [March 18, 2020, 3:20pm UTC](https://forum.ansible.com/t/search-upto-first-occurence-of-the-pattern/32496/1 "2020-03-18T15:20:19Z")

</div>

I have the output of command as below

/ps/was-ps/woa/woa\_runtime/profiles  
/ps/was-ps/woa12/woa\_runtime/profiles  
/pas/ec1/ukpl/woa\_runtime/profiles

> From this paths, I need to find the lines upto first occurence of woa or first occuerence of woa\*.

Output needed:  
/ps/was-ps/woa/  
/ps/was-ps/woa12/  
/pas/ec1/ukpl/woa\_runtime/

Is there anyway to do this in ansible regex\_search or anyother filter.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 18, 2020, 4:52pm UTC](https://forum.ansible.com/t/search-upto-first-occurence-of-the-pattern/32496/2 "2020-03-18T16:52:46Z")

</div>

Given the output lines are stored in the list "mylines", try this

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ mylines|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;map('regex\_replace', myregex, myreplace)|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myregex: '^(.\*?)woa(.\*?)/(.\*)$'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myreplace: '\g\<1\>woa\g\<2\>/'

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 19, 2020, 10:38am UTC](https://forum.ansible.com/t/search-upto-first-occurence-of-the-pattern/32496/3 "2020-03-19T10:38:28Z")

</div>

> Thank you for your response, this is giving output as below
> 
> \/ps/was-ps/woa/woa\_runtime/profiles  
> \/ps/was-ps/woa12/woa\_runtime/profiles  
> \/pas/ec1/ukpl/woa\_runtime/profiles

The playbook below works for me

> cat playbook.yml

- hosts: localhost  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;mylines:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- /ps/was-ps/woa/woa\_runtime/profiles  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- /ps/was-ps/woa12/woa\_runtime/profiles  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- /pas/ec1/ukpl/woa\_runtime/profiles  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ mylines|map('regex\_replace', myregex, myreplace)|list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myregex: '^(.\*?)woa(.\*?)/(.\*)$'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myreplace: '\g\<1\>woa\g\<2\>/'

> ansible-playbook playbook.yml

...  
TASK [debug] \*\*\*  
ok: [localhost] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/ps/was-ps/woa/",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/ps/was-ps/woa12/",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"/pas/ec1/ukpl/woa\_runtime/"  
&nbsp;&nbsp;&nbsp;&nbsp;]  
}  
...

HTH,  
&nbsp;&nbsp;-vlado
