I am building a Rails 5.2 app. In this app I parse incoming emails. In each email there is a tag connecting it to another object within the system.
It looks like this:
[Invitation_77decd78-0f77-46df-ae1c-e49f45a2a3ee]
Please note that both Invitation and 77decd78-0f77-46df-ae1c-e49f45a2a3ee are dynamic. But the [] are always there, encapsulating the substring.
How can I find the above string and then extract it from a longer text (the email body)?
CodePudding user response:
You may use a regex approach with a capture group:
s = "blah blah [Invitation_77decd78-0f77-46df-ae1c-e49f45a2a3ee] blah"
output = s.match(/\[\w*(\w{6}(?:-\w{4}){3}-\w{12})\]/)[1]
puts output # decd78-0f77-46df-ae1c-e49f45a2a3ee
To also capture the portion leading up to the UUID, add another capture group:
output = s.match(/\[(\w )_\w*(\w{6}(?:-\w{4}){3}-\w{12})\]/)
puts output[1] # Invitation
puts output[2] # decd78-0f77-46df-ae1c-e49f45a2a3ee