0

I am trying to use the replace module on a text file to replace any instances of the regex matches that it finds (NOT including the 's around it). I am basically just searching for an entire string or number (3306) or (myhostname) inside of a text file.

Unfortunately, the regex I am defining here is not detecting the string correctly. The examples I am finding on regextester seem to not be working correctly.

  tasks:
    - name: Rename the host database strings inside of each tenants settings.php file
      replace:
        dest: /var/www/html/someFile.txt 
        regexp: "/ec2-52-11-164-22.compute-1.amazonaws.com/g"
        replace: 127.0.0.1


    - name: Rename the port inside of each tenants settings.php file
      replace:
        dest: /var/www/html/someFile.txt 
        regexp: "/3306/g"
        replace: 4006

This is the text I am searching through -

Server1
        'host' => 'ec2-52-11-164-22.compute-1.amazonaws.com',
        'port' => '3306',
Server2
        'host' => 'ec2-34-112-123-53.compute-1.amazonaws.com',

What would be the proper regex to select those two strings without with 's?

J. Doe
  • 1,209
  • 5
  • 20
  • 43
  • Thanks! It makes it a lot easier to spot problems in your code (assuming that's where it is) if you post the actual code. – Fund Monica's Lawsuit Jun 23 '16 at 05:11
  • Well, the text I am searching through is just a test document that I made in order to test if the module was working, so whats in the original question is accurate. The code that is inside of the question is the Ansible code that I am running, which connects to the server but does not change anything (no changes since no regex matches). Essentially what you see there is all I am trying to do. – J. Doe Jun 23 '16 at 05:15
  • Alright, I answered as well as I could. I _think_ it should be correct, but tbh I'm not sure; you'll have to test it. – Fund Monica's Lawsuit Jun 23 '16 at 05:42

1 Answers1

1

You're trying to match two different strings to a single one. In regex, only a few characters have any special meaning; for a detailed guide on how to regex, this is my favorite.

Anyway, the gist of what you want to do is replace all of the groups of digits with something that the regex engine will match with a group of digits, which means this:

ec2(-\d+){4}

instead of this:

ec2-52-11-164-22

Now, that regex just means "match the characters e, c, and 2 literally, then match a subpattern four times. That subpattern matches a literal - character, followed by at least one digit". I'm not that familiar with Amazon AWS links, but for any part that changes between different hosts, you just replace it with the generic regex equivalent. For example, if the ec2 at the beginning can also be eb5 or ed78, it would be e[a-z]\d+ -- the letter e, followed by any character between the ASCII codes of a and z, followed by at least one digit.

As for the port, it would just be \d{1,4} -- anywhere from 1 to 4 digits. Unless you only want to match that specific port, of course, in which case keep what you have.

If you looked hard enough, you could probably also find a regex premade for you, but I'd recommend trying to build it yourself, with the help of tools like Regex101 for live testing and the guide I linked above for a reference. Regex is a valuable tool to have in your belt.

Community
  • 1
  • 1
Fund Monica's Lawsuit
  • 5,770
  • 8
  • 46
  • 65
  • Thanks for the help (and for tolerating my noobishness) – J. Doe Jun 23 '16 at 05:55
  • @J.Doe I don't think anyone minds noobishness except really, really grumpy people. What they mind is when a noob is unwilling to at least put in some effort, and you've shown yourself not to be that kind of person. :) – Fund Monica's Lawsuit Jun 23 '16 at 05:59