Solved

RegEx match a phrase but Replace only inner part

Posted on 2009-06-29
4
345 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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