-1

Given string

1201-ryryryry

I want to extract 01, only characters in position 3-4

With https://regex101.com/

I can regex

^(?:.{2})(.{2})

Which returns: Full match 0-4 1201 Group 1. 2-4 01

Using Ansible regex_search() and it appears it only reads the "Full match" of 1201.

Any idea how I can have it match only 01?

techraf
  • 64,883
  • 27
  • 193
  • 198
jacksonp
  • 7
  • 1
  • 6

2 Answers2

0

Using AnsibleĀ regex_search()

Do you want to simply get a substring? Why would you use a regular expression filter for that?

Use regular Python slicing in Jinja2 template:

string[x:y]

x points to the position from which to include; y points to the position from which to cut off -- in your case to get 01 (you can, of course, use a variable instead of the string directly):

- debug:
    msg: "{{ '1201-ryryryry'[2:4] }}"
techraf
  • 64,883
  • 27
  • 193
  • 198
0

Try Positive LookBehind

(?<=\d{2})\d{2}
Ibrahim
  • 6,006
  • 3
  • 39
  • 50