Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

RegEx match a phrase but Replace only inner part

Posted on 2009-06-29
4
Medium Priority
?
348 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 1000 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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

916 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