Rachela Meadows
Rachela Meadows

Reputation: 815

How to find all instances of @[XX:XXXX] in a string and then find the surrounding text?

Given a string like:

"@[19:Sara Mas] what's the latest with the TPS report? @[30:Larry Peters] can you help out here?"

I want to find a way to dynamically return, the user tagged and the content surrounding. Results should be:

user_id: 19
copy: what's the latest with the TPS report?

user_id: 30
copy: can you help out here?

Any ideas on how this can be done with ruby/rails? Thanks

How is this regex for finding matches?

@\[\d+:\w+\s\w+\]

Upvotes: 1

Views: 344

Answers (2)

FailedDev
FailedDev

Reputation: 26930

result = subject.scan(/\[(\d+).*?\](.*?)(?=@|\Z)/m)

This grabs id and content in backreferences 1 and 2 respectively. For stoping the capture either @ or the end of string must be met.

 "
\\[         # Match the character “[” literally
(          # Match the regular expression below and capture its match into backreference number 1
   \\d         # Match a single digit 0..9
      +          # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
.          # Match any single character that is not a line break character
   *?         # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
\\]         # Match the character “]” literally
(          # Match the regular expression below and capture its match into backreference number 2
   .          # Match any single character that is not a line break character
      *?         # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
)
(?=        # Assert that the regex below can be matched, starting at this position (positive lookahead)
              # Match either the regular expression below (attempting the next alternative only if this one fails)
      \@          # Match the character “\@” literally
   |          # Or match regular expression number 2 below (the entire group fails if this one fails to match)
      \$          # Assert position at the end of the string (or before the line break at the end of the string, if any)
)
"

This will match something starting from @ and ending to punctuation makr. Sorry if I didn't understand correctly.

result = subject.scan(/@.*?[.?!]/)

Upvotes: 1

Ryanmt
Ryanmt

Reputation: 3265

Split the string, then handle the content iteratively. I don't think it'd take more than:

tmp = string.split('@').map {|str| [str[/\[(\d*).*/,1], str[/\](.*^)/,1]] }
tmp.first #=> ["19", "what's the latest with the TPS report?"]

Does that help?

Upvotes: 2

Related Questions