1

I need to grab the first occurrence of this pattern [some-file-name ci] OR [some-file-name-ci.yml ci]

So far I'm able to create this pattern

CONFIG_FILE_PATTERN = /\[(\w*[ _-]ci|\w*[ _-]*\.yml ci|.\w*[ _-]*\.yml ci|.\w*[-_]\w*[ _-]*\.yml ci|\w*[-_]\w*[ _-]*\.yml ci|\w*[-_]\w*[ _-]*\w ci|.\w*[-_]\w*[ _-]*\w ci)\]/i.freeze

This works fine for all cases except when there are multiple hyphens (-) in the file name.

Examples:

[hello-ci.yml ci] # works fine for this
[hello.yml ci] # works fine for this
[hello ci] # works fine for this
[hello-world-ci.yml ci] # does not work for this as the file name now have multiple hyphens

Any help will be appreciated.

3
  • 1
    I think all you need is /\[([^\]\[]*ci)\]/i. See a Rubular demo. Or, /\[([^\]\[]*[ _-]ci)]/i if there must be space, _ or - before ci Commented May 6, 2020 at 8:45
  • Thank you, I do need a space between the filename and ci. The second one worked for me @Wiktor Commented May 6, 2020 at 9:10
  • This is the regex I posted, see the answer below, please consider accepting/upvoting. Commented May 6, 2020 at 9:13

1 Answer 1

1

You may use

/\[([^\]\[]*[ _-]ci)\]/i

See the Rubular demo

Details

  • \[ - a [ char
  • ([^\]\[]*[ _-]ci) - Group 1:
    • [^\]\[]* - 0+ chars other than [ and ]
    • [ _-] - a space, _ or -
    • ci - a ci substring
  • \] - a ] char.

A Ruby test at IDEONE to extract the first occurrence:

s = '[hello-ci.yml ci] works fine for this [second-DONT-EXTRACT-hello-ci.yml ci]'
puts s[/\[([^\]\[]*[ _-]ci)\]/i, 1] # => hello-ci.yml ci
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.