0

I am trying to replace the unnecessary words from the below string using regex_replace but unable to do to so for the last part of it.

String: Test_[u'Net::Route Domain: RD2001']

Regex being used: regex_replace('.*: (.*)$', '\\1') 
Output printing: Test_RD2001']

Expected Output: Test_RD2001

Could someone suggest how can I get rid of the last characters after RDXXXX. Also this RDXXXX number is dynamic.

5
  • u'Net::Route Domain: RD2001' is suspicious looking. How does this string get built in the first place, because it looks like python printing an internal representation of a unicode string rather than normal string variable contents, or [] looks like you are printing an array containing the string Net::Route... as a string itself Commented Jun 22, 2021 at 13:47
  • Actually it is an output from an "item.stdout_lines[0]" which I am logging into a file using blockinfile module. Not sure from where these crazy characters getting appended. Ideally the output I get is "Net::Route Domain: RD2001" and I a just need RD2001 value. Commented Jun 22, 2021 at 13:51
  • @P.... Thanks alot yes it worked. I mark as accept Commented Jun 24, 2021 at 5:23
  • @P.... could u tell me what exactly does this part of regexp does.. '^(\\w+) Commented Jun 24, 2021 at 12:37
  • Put these comments below the answer to contact the answerer - but ^ matches the start of the string so nothing comes before, and \w+ matches one or more "word characters", non-whitespace, non-punctuation, so together they match and capture the word Test_ at the start of the string, before the first [u' Commented Jun 24, 2021 at 12:51

1 Answer 1

2

As dicussed in comments, you should try to fix the data source. However, here is the regex to print the expected result:

---

- name: Sample playbook
  connection: local
  #gather_facts: false
  hosts: localhost
  vars:
    String: "Test_[u'Net::Route Domain: RD2001']"
  tasks:
  - debug:
      msg: "{{ String| regex_replace('^(\\w+)\\[.*:\\s+([\\w]+).*', '\\1\\2') }}"

Results:

PLAY [Sample playbook] ***************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************
ok: [localhost]

TASK [debug] *************************************************************************************************************************
ok: [localhost] => {
    "msg": "Test_RD2001"
}

PLAY RECAP ***************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.