Solved

RegEx match a phrase but Replace only inner part

Posted on 2009-06-29
4
341 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
  • 2
  • 2
4 Comments
 

Author Comment

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

Accepted Solution

by:
abel earned 250 total points
Comment Utility
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
Comment Utility
> 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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now