Regular Expression Help Needed

Hello,

This forum has been very helpful in solving my newbie questions. Thanks!
Now I need help again with this:

I have an array that contains concatenated names of certain keystrokes:

keystrokes=[
VK_A, # the letter a on the keyboard
VK_A_CTRL_SHIFT, # Ctrl+Shift+a
VK_A_SHIFT, # Shift+a
VK_DOWN, # the ArrowDown key
VK_DOWN_SHIFT # Shift+ArrowDown
]

I am looking for a regex that returns the ‘VK_A’ part in the first 3
cases and ‘VK_DOWN’ in the last 2 cases.

-X

keystrokes= %w[
VK_A
VK_A_CTRL_SHIFT
VK_A_SHIFT
VK_DOWN
VK_DOWN_SHIFT
]

keystrokes= %w[
VK_A
VK_A_CTRL_SHIFT
VK_A_SHIFT
VK_DOWN
VK_DOWN_SHIFT
]

results = keystrokes.map do |str|
if md = str.match(/
\A #start of string
[^]+ #not an underscore 1 or more times
_ #an underscore
[^
]+ #not an underscore 1 or more times
/xms)

md[0]

else
‘’
end

end

–output:–
[“VK_A”, “VK_A”, “VK_A”, “VK_DOWN”, “VK_DOWN”]

7stud – wrote in post #1006056:

This is easier to read:

keystrokes= %w[
VK_A
VK_A_CTRL_SHIFT
VK_A_SHIFT
VK_DOWN
VK_DOWN_SHIFT
]

results = keystrokes.map do |str|
md = str.match(/
\A #start of string
[^]+ #not an underscore 1 or more times
_ #an underscore
[^
]+ #not an underscore 1 or more times
/xms)

if md
md[0]
else
‘’
end

end