Need to identify and replace content between { }'s

Hello.

I am attempting to match the following:

{ some_text -> a_url }

where 'some_text' is a string of characters of any length and 'a_url' is obviously a URL.
Within a body of text, this pattern may be found multiple times.

I came up with the regex:

\{(.+)->(.+)\}

Here was my thinking:

\{  - match the opening curly bracket
(.+)  - capture  text between the curly bracket and the arrow, ->
->  - match the arrow
(.+)  - capture  the text between the arrow and the closing curly bracket
\}  - match the closing curly bracket.

Ultimately, I am trying to convert the contents of the matched by the pattern into a typical <a> tag.  That's why I'm storing 'some_text' and 'a_url'.  The 'some_text' becomes enclosed by the <a> tag and the 'a_url' becomes assigned to href attribute.

I thought I had it figured out.  When I tested the regex on  text containing one instance of the pattern, things went smoothly like I had anticipated.  However, when multiple instances occur the regex matches a huge chunk of text encompassing the first opening curley bracket ( { ) found and the last closing curly bracket ( } ) found.

I understand that I need to make my expression more precise, but I am at a loss as to how I can accomplish this.

How do I more precisely define the regex so that I can indentify multiple instances of the { some_text -> a_url } pattern?

Many thanks,
Eric
ewolsingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ddrudikCommented:
Please provide an example of your real source text, with multiple instances (if exist) etc.
0
ewolsingAuthor Commented:
I have attached a .txt file containing an example text with two instances of the pattern I described earlier.
sample.txt
0
ddrudikCommented:
Thanks, consider this example:
http://www.myregextester.com/?r=35

Raw Match Pattern:
"{(.*?)->(.*?)}"

Raw Replace Pattern:
<a href="$2">$1</a>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ewolsingAuthor Commented:
Thanks!  That did the trick.  I really appreciate it.

For my own edification, let me parse your reasoning on the '(.*?)' segments.

.  - match any character...
*  - ...as many times as necessary...
?  - ...only once before the arrow (->).

Do I have that correct?

Thanks again.
0
ddrudikCommented:
Yes, from that site link there's an "explain" checkbox that returns this upon submit:

Match Pattern Explanation:
The regular expression:

(?-imsx:"{(.*?)->(.*?)}")

matches as follows:
 
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  "{                       '"{'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  ->                       '->'
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  }"                       '}"'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.