Conditional REGEX replace of apostrophe works in EditPad & regex101.com but not in seven years old UEStudio 15.30.0.15

Conditional REGEX replace of apostrophe works in EditPad & regex101.com but not in seven years old UEStudio 15.30.0.15

3
NewbieNewbie
3

    Apr 17, 2023#1

    I want to use REGEX to modify text strings that identify photos to produce HTML code to display the photos. Some of the identifying strings contain apostrophes and need to be escaped in the HTML code with \. I have REGEX search and replace functions that work in EditPad and also on the regex101.com site, but I can't get to work with UEStudio. 

    Examples of text to be searched and modified:

    Code: Select all

    ~Photos caption~(Jun, 2020)~Photo1.jpg
    ~Photo's caption~(Jun, 2020)~Photo2.jpg
    REGEX search: ^~(\w*)(')*(.*)~(.*)~(.*)

    REGEX replace: '<figure><img src="$5" width="100%">$1${2:+\\':}$3 $4</figure>',

    Expected result but doesn't work in UEStudio:

    Code: Select all

    '<figure><img src="Photo1.jpg" width="100%">Photos caption (Jun, 2020)</figure>',
    '<figure><img src="Photo2.jpg" width="100%">Photo\'s caption (Jun, 2020)</figure>',
    What am I doing wrong in UEStudio?

    Windows 10
    UEStudio '15 Development Environment (x64) 
    Version 15.30.0.15
    Perl engine

    18672
    MasterMaster
    18672

      Apr 17, 2023#2

      Hi DougEamon,

      There are many different Perl regexp libraries. UES, UE and some other editors use Boost Perl regexp library which has a different syntax for Replace Conditionals. Unfortunately UE(S) does not support this feature. Conditionals work only in the search pattern.

      BR, Fleggy

      3
      NewbieNewbie
      3

        Apr 17, 2023#3

        Thanks for the info. UE is such a great editor in every other respect it surprises me that it lacks this feature. 
        -Doug

        6,602547
        Grand MasterGrand Master
        6,602547

          Apr 17, 2023#4

          I am not aware of support for conditional replaces by the regular expression engine in Perl compatible syntax of the Boost library used by UltraEdit and UEStudio in various library versions depending on version of UE/UES. I could not read anything regarding to conditional replacement support in even the currently latest version of Boost Perl Regular Expression Syntax documentation which seven years old UEStudio v15.30 has not embedded. There is support for conditional expressions for matching strings, i.e. conditions in search expressions. The posted conditional replace expression does also not produce the expected result with currently latest UEStudio v2022.2.0.52.

          The simple solution is running two Perl regular expression replaces in UEStudio instead of just one.
          1. Find what: ^~([^~'\v]+)('.*?)~(.*?)~(.*)$
            Replace with: '<figure><img src="\4" width="100%">\1\\\2  \3</figure>',
            or
            Replace with: '<figure><img src="$4" width="100%">$1\\$2  $3</figure>',
          2. Find what: ^~(.*?)~(.*?)~(.*)$
            Replace with: '<figure><img src="\3" width="100%">\1\ \2</figure>',
            or
            Replace with: '<figure><img src="$3" width="100%">$1\ $2</figure>',
          I hope, there is never a caption containing more than one apostrophe because none of the regular expressions posted in this topic until now would handle such captions correct. I would use an UltraEdit script to first replace all image references to HTML format and search in a loop for image references with a caption with at least one apostrophe without a backslash left to it and replace all the apostrophes without an escaping backslash in the image caption with a backslash and the apostrophe. It would be also possible to do the opposite and add all the missing backslashes first by searching for image references with one or more apostrophe without a backslash before in the caption using a loop and next run a replace to reformat all image references with now correct captions to HTML format.

          BTW: I think, it would be better to replace all apostrophes in an image caption by &#39; or &#x27; or &apos; whereby the last would be my favorite.
          Best regards from an UC/UE/UES for Windows user from Austria

          3
          NewbieNewbie
          3

            Apr 17, 2023#5

            Thank you, Mofi. I did consider running two regex expressions, but your suggestion is more complete. Still, for me it is simpler just to use the alternate editor, NotePad. Thanks also for your suggestion to use &apos;, something I plan to do.

            18672
            MasterMaster
            18672

              Jun 16, 2023#6

              Hi Mofi,

              AFAIK Notepad++ also uses Boosts library and conditional replace works in it. For example this simple test:
              F: (lo)|mo
              R: (?1matched:unmatched)
              in text hello Mofi  -> helmatched unmatchedfi    :)

              BR, Fleggy

              6,602547
              Grand MasterGrand Master
              6,602547

                Jun 16, 2023#7

                Hi Fleggy,

                I don't know which version of the Boost library collection (there are multiple libraries) is issued by UltraEdit and which functions are used by UltraEdit from which of the Boost libraries with which flags and and parameters. I have not installed ever Notepad++ nor do I know anything its source code or its features. I searched once more for conditional expression support in the Boost documentations and found the User's Guide with:
                Boost User's Guide wrote:The Boost-Specific Format Sequences

                When specifying the format_all flag to regex_replace(), the escape sequences recognized are the same as those above for format_perl. In addition, conditional expressions of the following form are recognized: 

                Code: Select all

                ?Ntrue-expression:false-expression
                where N is a decimal digit representing a sub-match. If the corresponding sub-match participated in the full match, then the substitution is true-expression. Otherwise, it is false-expression. In this mode, you can use parens () for grouping. If you want a literal paren, you must escape it as \(.
                However, I have never needed conditional replaces in the last 25 years and I am quite sure that I will not need them in the next 25 years. What can be achieved with a conditional replace can be always achieved with two or more simple regular expression replaces, too. I favor simple solutions over complex solutions wherever possible although parts of mankind are working hard on making our life more complex year by year. I have my doubts if that is really useful for the majority of the people or just for a small percentage.

                I like the blog Is software getting worse? written by Isaac Lyman. When I think what was done by me and many other programmers 20 or 30 years ago with the hardware available in the 1990s and 2000s and how good the programs were written to do what the users really needed and what programmers produce nowadays running on hardware being so much better, I think, the digital world is developed in the completely wrong direction. Conditional regular expression replacements is in my opinion a feature of which the mankind could easily go without it.
                Best regards from an UC/UE/UES for Windows user from Austria