Problem with using regular expression to find and replace

Hey there. I am trying to use a regular expression to find and replace '_' characters with '-', but I only want to replace those underscores if that text string is within curly braces. For instance, say I have the following:

This_is_a_sentence_with_underscores_{but_only_want_to_replace_these}_and_no_others.

And after replacing the pertinent underscores, I have this:

This_is_a_sentence_with_underscores_{but-only-want-to-replace-these}_and_no_others.

Can I do this using a regular expression using DOORS search and replace, or do I need to write a DXL script? I've tried using a regular expression using the DOORS search and replace, but all the underscores get replaced, not just those within the curly braces. Can anyone help?

Thanks!!!

David
SystemAdmin - Wed Dec 12 14:50:22 EST 2012

Re: Problem with using regular expression to find and replace
MichaelGeorg - Fri Dec 14 03:17:09 EST 2012

Hi David,

I don't think it is possible to do what you want with the DOORS search and replace function.
Although you can search for '_' inside the curly braces with regular expression \{.*_.*\}, you can not replace the '_' this way, as replace will always replace the complete expression and not only the '_'s.

- Michael

Re: Problem with using regular expression to find and replace
Peter_Albert - Fri Dec 14 03:58:16 EST 2012

MichaelGeorg - Fri Dec 14 03:17:09 EST 2012
Hi David,

I don't think it is possible to do what you want with the DOORS search and replace function.
Although you can search for '_' inside the curly braces with regular expression \{.*_.*\}, you can not replace the '_' this way, as replace will always replace the complete expression and not only the '_'s.

- Michael

A simple and straight-forward DXL routine could lool like this:

string myReplace(string sInput){
  int    i
  char   c
  Buffer B
  string sReturnValue
  bool   bBetweenBrackets
  B = create
  B = ""
  bBetweenBrackets = false
  for i in 0:length sInput -1 do {
    c = sInput[i]
    if      (c == '{'){bBetweenBrackets = true}
    else if (c == '}'){bBetweenBrackets = false}
    B += bBetweenBrackets ? (c == '_' ? '-' : c) : c
  }
  sReturnValue = stringOf B
  delete B
  return sReturnValue
}
 
string s = "This_is_a_sentence_with_underscores_{but_only_want_to_replace_these}_and_no_others."
print myReplace(s)

It does not check for specific cases, like sentences with only one opening bracket, but it is a start.

Cheers,

Peter

Re: Problem with using regular expression to find and replace
llandale - Fri Dec 14 11:06:10 EST 2012

Albert's solution seems adequate. If you wanted a Regexp solution, I suppose it would be outlined like this:

Regexp reBracedText = regexp2(something that finds text between braces "{" and "}")
   // match 1: Any number of characters that does not include '{'; ends with '{'
   // match 2: Any number of characters up to be excluding the first '}' character
   // match 3: Any number of characters that starts with '}'
 
RemainingText = OriginalText
bufResults = ""
 
while (!null RemainingText)
{  if (!reBracedText RemainingText)
   {  bufResults += RemainingText
      RemainingText = ""
   }
   else
   {  bufResults += RemainingText[match 1] // part up to including the first brace
      bufResults += ReplaceUnderscoresToDashes(RemainingText[match 2])
      RemainingText = RemainingText[match 3]
   }
}
AdjustedtText = stringOf(bufResults)

 

  • I suspect you will need a "non-greedy" Regexp search (Wikipedia "Regular Expressions")
  • you need to test mis-matched and nested brace conditions.
    • Try "A{B_{C_}D_{E_}". Which I think incorrectly becomes "A(B-(C-}D_{E-}"
  • If you want MATCHING braces you'll need a recursive algorithm


-Louie