Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

How can I replace text inside angle brackets?

 

My best try so far:

       Dim Input As String = "Aaaa [abc] Bbbb [7] Ccc"
       Dim Output As String = Regex.Replace(Input, "\[.+\]\s", "")
       ' Output is "Aaaa Ccc" but should be "Aaaa Bbbb Ccc"

  • Leaders
Posted

This is an issue of greedy versus lazy quantifiers. Basically, a greedy quantifier, such as *, will try to match as much as possible. Your regex describes a string with brackets on either end, which does match the string "[abc] Bbbb [7]". You want the lazy version, which will find the smallest match possible.

 

Check out http://www.regular-expressions.info/reference.html

*? (lazy star) Repeats the previous item zero or more times. Lazy, so the engine first attempts to skip the previous item, before trying permutations with ever increasing matches of the preceding item.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...