Topic
  • 3 replies
  • Latest Post - ‏2012-12-14T16:06:10Z by llandale
SystemAdmin
SystemAdmin
3180 Posts

Pinned topic Problem with using regular expression to find and replace

‏2012-12-12T19:50:22Z |
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
Updated on 2012-12-14T16:06:10Z at 2012-12-14T16:06:10Z by llandale
  • MichaelGeorg
    MichaelGeorg
    53 Posts

    Re: Problem with using regular expression to find and replace

    ‏2012-12-14T08:17:09Z  
    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
  • Peter_Albert
    Peter_Albert
    250 Posts

    Re: Problem with using regular expression to find and replace

    ‏2012-12-14T08:58:16Z  
    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

    Updated on 2014-01-06T14:21:03Z at 2014-01-06T14:21:03Z by iron-man
  • llandale
    llandale
    2972 Posts

    Re: Problem with using regular expression to find and replace

    ‏2012-12-14T16:06:10Z  

    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

     

    Updated on 2014-01-06T14:21:15Z at 2014-01-06T14:21:15Z by iron-man