1

I'd like to parse string for two different tags then store each in the database. Let's call these tag1 and tag2. I have a delimeter of sorts, "?#" that is the split between tag1 and tag2.

Suppose

t = "random text blah firsttag?#secondtag more blah"

Goal: tag1 should be "firsttag" and tag2 should be "secondtag" without the preceding or trailing random text. Each should get stored as objects in the database.

I tried something like:

t.split

but it returns

["random text blah firsttag", "secondtag more blah"]

and includes the random text. How can I get the split to stop when it reaches the first space in either direction?

I'd like this to also work if there are multiple tag pairs in the string, for example, if:

m = "random firsttag#?secondtag blah blah 1sttag#?2ndtag blah blah blah"

I'm pretty new to both ruby and rails, so I really appreciate your help on this one!

1 Answer 1

3

You can use a regular expression combined with split:

tags = t.match(/\S+\?#\S+/)[0].split('?#')

Explanation

First let's capture the interesting part of the text, which is tag?#tag2. We'll use a regex for that:

/\S+\?#\S+/

Explanation:

\S+ captures non-whitespace characters preceding the delimiter (tag)

\?# captures the delimiter token

\S+ captures all trailing non-whitespace chars (tag2)

The match of that regex (which we access with [0] is:

firsttag?#secondtag

Then from that we just split using the delimiter and we get the array with the tags.

Cheers!

PS: I've retagged the post since it has nothing to do with ruby-on-rails. It's just plain ruby

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Pablo, I found especially helpful the regex walk-through!
Follow-up question: How might I be able to store those parsed snippets in the database? To be specific, I'm using the Twitter gem to retrive and store tweets in a database. I'd like to be able to parse the tweets in the manner described above and store the snippets as instances in a related object class I've created. Any ideas?
If you need a small abstraction over your DB I'd recommend looking at Sequel sequel.rubyforge.org

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.