Reputation: 141
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.
Upvotes: 1
Views: 60
Reputation: 627468
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
Upvotes: 1