Solved

RegEx match a phrase but Replace only inner part

Posted on 2009-06-29
4
346 Views
Last Modified: 2012-05-07
Hi,
Please look at the example code:

Const word_separation_chars As String = "^A-Z !#"
Dim search_str As String = "HELLO"
Dim reg_str As String = String.Format("([{0}]{1}[{0}])", word_separation_chars, search_str)

Dim input_line As String = " !HELLO WORLD# "
Dim marked_line As String = Regex.Replace(input_line, reg_str, "<span>$1</span>")

Will result:
" <span>!HELLO<span> WORLD# "

But what I need is to capture only the "HELLO" part (without the separation character) i.e.:
" !<span>HELLO<span> WORLD# "

How should I do that elegantly?
Thanks, Aryeh.
0
Comment
Question by:tuchfeld
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Author Comment

by:tuchfeld
ID: 24740194
Note that the search_str can apear several times inside the input_line.
0
 
LVL 39

Accepted Solution

by:
abel earned 250 total points
ID: 24740539
what have now is:

Regex.Replace(input_line, "([^A-Z !#]HELLO[[^A-Z !#])", "<span>$1</span>")
you can change that to the following:

Regex.Replace(input_line, "([^A-Z !#])(HELLO)([[^A-Z !#])", "$1<span>$2</span>$3")
Or, in other words, in your third line:

Dim reg_str As String = String.Format("([{0}])({1})([{0}])", word_separation_chars, search_str)

-- Abel --
0
 
LVL 39

Expert Comment

by:abel
ID: 24740544
> Note that the search_str can apear several times inside the input_line.
that doesn't matter, it will automatically replace all occurrences for each time the whole regex is matched.
0
 

Author Comment

by:tuchfeld
ID: 24743035
FYI, if anyone might find it useful, here is the RegEx I eventually used (it includes match at the beginning and end of input).
Aryeh.
Const word_separation_chars As String = "^a-z"
Dim reg_str As String = String.Format("((^|[{0}])({1})([{0}]|$))", word_separation_chars, search_str)

Open in new window

0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question