• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2186
  • Last Modified:

C# Regex Remove text in middle of string

I have a string of the form
2009-04-18 01:02:03 word1 word2 word3 leave this text alone

I'd like to remove "word1 word2 word3".  This would be easy if those words were at the beginning or end of the string, because I could use start-of-line or end-of-line anchors.

Is there a technique for finding a location, such as the start of word1, in a string and using then that location as one would use an anchor?

Do I have the wrong idea and should be going at it a different way?

0
josgood
Asked:
josgood
  • 5
  • 2
1 Solution
 
abelCommented:
If you mean to say that you do not know beforehand how word1/2/3 are looking like, nor you know anything about how "leave this text alone" looks like, it gets a bit difficult. But assuming that:

  • The word1/2/3 are separated by one ore more spaces
  • the start is a date/time stamp of always the same length
  • after removing word1/2/3 you want one space and not two (as in your example)
then you can use this matching regular expression:

^.{20}([^ ]+ ){3}
and you add a few capturing parentheses and you can use it like this:

string replacedString = Regex.Replace(yourString, "^(.{20})(([^ ]+ ){3})(.*)", "$1$4");

-- Abel --
0
 
abelCommented:
Just tested it to be sure. The following works:

//
// replacing a string using a regex
//
string yourString = "2009-04-18 01:02:03 word1 word2 word3 leave this text alone";
string replacedString = Regex.Replace(yourString, "^(.{20})(([^ ]+ ){3})(.*)", "$1$4");
//
// replacedString now contains "2009-04-18 01:02:03 leave this text alone"
//

Open in new window

0
 
josgoodAuthor Commented:
Thank you!  That works great.

I almost understand.
^(.{20}) collects the first 20 characters as $1
([^ ]+ ){3}) collects three words
(.*) collects the rest of the string as $4

$1$4 concatenates the first 20 characters and the $4 "rest of the string" to form the replacement string.

I'm probably exhibiting a fundamental mis-understanding of something, but I would think that
   ([^ ]+ ){3})
would collect three words as $2, $3, and $4.

Could you clarify me for?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
abelCommented:
> Could you clarify me for?

yes, I can. If you work with regular expressions, then each opening parenthesis introduces a new group grouping expression and thus a new group. Simply put, counting the opening parentheses from left to right will show you which $x you need.

then: $0 means "everything that matches" which is handy when your match does not match the whole string but only parts.

then: (?:somesub-expression-here) introduces non-capturing groups, these do not count into a $1, $2 etc. The start with (?:

just as a thought or mnemonic. Suppose the expression would've been  (([^ ]+ ){3,8}) which means 3 to 8 words, how would you know what to put in the replacement expression if every caught word would end up in a new $x?

In .NET it is possible to enter the layered grouping expressions through code. And more advanced replacement expressions also exist. But for now, just remember to count the parentheses, it works in almost every regular expression flavor (javascript, java, .net, perl, ruby etc).

-- Abel --
0
 
abelCommented:
PS: good tutorials and references can be found here: http://regular-expressions.info
0
 
josgoodAuthor Commented:
I'm always impressed by simplicity.

A real expert can explain to you, in words you understand, the actual simplicity behind an apparent complexity.  A real expert shows you why things are simple.

Thank you for doing so for me.

It is *so* cool when you understand something that has been a problem for you!

Joe
0
 
abelCommented:
tx a bunch for the nice compliment! :-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now