Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find and extract string from text?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I find the above string and then extract it from a longer text (the email body)?

>Solution :

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{6}(?:-\w{4}){3}-\w{12})\]/)
puts output[1]  # Invitation_77
puts output[2]  # decd78-0f77-46df-ae1c-e49f45a2a3ee
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading