# Regex for catching null-byte string

**URL:** <https://forum.ansible.com/t/regex-for-catching-null-byte-string/40038>\
**Category:** Get Help\
**Tags:** ansible-lint, ansible-core\
**Created:** [February 4, 2025, 11:04am UTC](https://forum.ansible.com/t/regex-for-catching-null-byte-string/40038 "2025-02-04T11:04:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![EasyMoney322](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/easymoney322/32/7479_2.png) [@EasyMoney322](https://forum.ansible.com/u/EasyMoney322)\
**Post date:** [February 4, 2025, 11:04am UTC](https://forum.ansible.com/t/regex-for-catching-null-byte-string/40038/1 "2025-02-04T11:04:47Z")

</div>

Hello,  
I’ve a file that may or may not be start with line filled with null-bytes (\u0000\u0000…).  
I’ve written a conditional that was supposed to catch it  
`positions['content'] | b64decode | regex_search('^\\0+$')`  
But I get an error  
`fatal: [REDACTED]: FAILED! => {"msg": "The conditional check positions['content'] | b64decode | regex_search('^\\0+$') failed. The error was: Invalid conditional detected: source code string cannot contain null bytes\n\nThe error appears to be in '/etc/ansible/tasks/MonitorTasks.yml': line 6, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Print returned information\n ^ here\n"}`

I’ve tried to replace it with `\u0000`, `\\u0000`, `\0` but I get the same error.  
I’m have to use `\\S` for now, but this expression also catches many valid cases.

How do I check for a string of null bytes, terminated by `\r` or `\n`?  
What’s the ansible syntax for regex?

UPD: Looks like `regex_search('^\\\u0000+$')` doing the job, but is it the best way to do it?

---

<div class="post-metadata">

**Author:** ![transcaffeine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/transcaffeine/32/7470_2.png) [@transcaffeine](https://forum.ansible.com/u/transcaffeine)\
**Post date:** [February 4, 2025, 3:41pm UTC](https://forum.ansible.com/t/regex-for-catching-null-byte-string/40038/2 "2025-02-04T15:41:39Z")

</div>

Without writing a custom filter, the way you formulated in your posts’ update is very likely the best way to currently do this.
