Domanda

I want to be able to capture the "host variable" of my project - the bit that says "mlocal" when I'm running the project locally, or mstage/m/user1234 when on the Moovweb cloud.

The variable $host contains the full URL of the site, so I can't use that. I want to be able to grab the first bit of the URL (example: mlocal.site.com - I want to grab "mlocal") and set it in a new variable.

I'm trying like so:

var("host") {
  $my_var = capture(/^\w+/)
}

But it doesn't seem to work.

È stato utile?

Soluzione

In order to capture the host variable only, you have to create a capture group using parentheses in your regex and then extract the host variable using the group number. Here's an example on how you would do it:

  $host {
    capture(/(\w+)\..*/) {
      $hostvar = $1
    }
  }
  log($hostvar)

This should log the host variable to the console. Notice how the regex in the parameter for the capture contains parenthesis around the first section, which is subsequently split by an explicit period, and then we match the rest of the string, but outside the capture group. Once we're within the scope of the capture, we can extract the capture group by using the $1 variable.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top