Search code examples
rubyregexpuppet

Ruby regex - stop when found first of two chars


Lets say that i have the following array with 4 elements:

myArray = [
    "elasticsearch/elasticsearch-analysis-icu/2.4.2",
    "lukas-vlcek/bigdesk/2.5.0",
    "royrusso/elasticsearch-HQ",
    "polyfractal/elasticsearch-inquisitor"
]

When iterating, I want to assign a substring to a variable, resulting in:

0) analysis-icu
1) bigdesk
2) HQ
3) inquisitor

I have tried the regex (.*\/(?:elasticsearch-)?(.*)(?:\/.*|\z)) but it doesnt stop at the /

How can i make it match until the end of each element string OR at the second /, if there is one?

Online tester here: http://rubular.com/r/t8ws1BbOU6


If you're into puppet, full code below:

elasticsearch::plugin {[
    "elasticsearch/elasticsearch-analysis-icu/2.4.2",
    "lukas-vlcek/bigdesk/2.5.0",
    "royrusso/elasticsearch-HQ",
    "polyfractal/elasticsearch-inquisitor"
]}

And:

$es_plugin_dir = regsubst($name, '(.*\/(?:elasticsearch-)?(.*)(?:\/.*|\z))', '\2')

Solution

  • It looks to me this regex should work for you:

    (^[^\/]*?\/(?:[^\/-]*?\-)?([^\/]*?)(?=\/[^\/]*?$|$).*)
    

    And here is your updated code:

    $es_plugin_dir = regsubst($name, '(^[^\/]*?\/(?:[^\/-]*?\-)?([^\/]*?)(?=\/[^\/]*?$|$).*)', '\2')