Solved

RegEx match a phrase but Replace only inner part

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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