Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regular expression help
#1
Hello All,

Need your help to understand what below python regular expression code does. It's from a Ansible playbook.

- { regex: '((?:hares|hagrp)\s+(?:-add|-modify|-link).*)repl(.*$)', replace: '\1vg\2' }


thanks in advance,
Anil
Reply
#2
there is a regex expression analyzer here
(first on on search, there are many others)
anilrajr likes this post
Reply
#3
(May-08-2024, 08:53 AM)Larz60+ Wrote: there is a regex expression analyzer here
(first on on search, there are many others)

for some reason, I'm not able to get the correct results using this. If someone can help on this, much appreciated. Thanks.
Reply
#4
If write in plain Python,look at regex101 right side has explanation.
So Ansible add some extra keyword like regex: and replace: that native to there package.
Try play around with code so eg most first match hares or hagrp...ect,if not will not replace repl.
import re

pattern = r"((?:hares|hagrp)\s+(?:-add|-modify|-link).*)repl(.*$)"
replacement = r"\1vg\2"
example_text = "hagrp -add some configuration repl with these settings"
modified_text = re.sub(pattern, replacement, example_text)
print( example_text)
print( modified_text)
Output:
hagrp -add some configuration repl with these settings hagrp -add some configuration vg with these settings
Reply
#5
You are replacing (prefix)repl(suffix) with (prefix)vg(suffix) if both prefix and suffix match a pattern.

The prefix pattern is ((?:hares|hagrp)\s+(?:-add|-match|-link).*)
hares|hagrp matches hares or hagrp. | is an OR
\s+ looks for one or more (+) whitespace characters (\s)
(?:) is needed to tell the OR that it will match hares or hagrp followed by \s+, not hares or hagrp\s+. The ?: makes this non-capturing.
The pattern is surrounded by parenthesis, so it is captured as a group.

The suffix pattern is (.*$)
.* captures any number of any character.
$ matches the end of the line.
The pattern is surrounded by parenthesis, so it is captured as a group.

replace: '\1vg\2' replaces the string with a new string if the pattern match was successful.

\1 refers to the first captured group, \2 the second. If the match was successful, the replacement string is:
(prefix group)vg(suffix group)

Using snippsat's example: "hagrp -add some configuration repl with these settings"
The prefix group is "hagrp -add some configuration "
The suffix group is " with these settings"
The new string is "hagrp -add some configuration " + "vg" + " with these settings"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  data validation with specific regular expression shaheen07 0 382 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  Regular Expression search to comment lines of code Gman2233 5 1,746 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,736 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Need help with my code (regular expression) shailc 5 2,006 Apr-04-2022, 07:34 PM
Last Post: shailc
  Regular Expression for matching words xinyulon 1 2,218 Mar-09-2022, 10:34 PM
Last Post: snippsat
  regular expression question Skaperen 4 2,577 Aug-23-2021, 06:01 PM
Last Post: Skaperen
  How can I find all combinations with a regular expression? AlekseyPython 0 1,709 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  Python Regular expression, small sample works but not on file Acernz 5 3,013 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,466 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Regular expression: return string, not list Pavel_47 3 2,570 Jan-14-2021, 11:49 AM
Last Post: Pavel_47

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020