In UltraEdit syntax
* means any number of occurrences of any character except newline characters. While this special character is often best, in some situations it is better to use
?++ which means the same, but behavior is in some situations different to
* like here.
^(*^) or
^(...^)*^(...^) results often in unexpected behavior while
^(?++^) or
^(...^)?++^(...^) produces the expected results.
* is (most often) a non greedy expression and matches therefore nothing if possible. With tags used on search string often the part of a found string matched by
* is not added to any tag although this is expected. With my years of experience in using UltraEdit regular expressions I don't wonder anymore and simply use
?++ or
?+ if
* results in a tagged expression in unexpected results.
On the other hand sometimes it is better to use
* instead of
?++ or
?+ as when the computer name only should be found in a UNC file name.
For example:
\\computer name\share\directory\file name\\?++\ or
\\?+\ can't be used to match just
\\computer name\ as these expressions are greedy and match therefore everything from first
\\ to last
\ on the line. So for this example it matches
\\computer name\share\directory\. That's good if the full path of a file name should be matched, but for matching just the computer name
\\*\ is required as this expression is non greedy and matches only
\\computer name\.
Okay, back to your problem. Because an
owner tag already exists in lots of files above the tags with first and last name, it would be good if a tagged regular expression Replace in Files could be used for that task. And yes, it is possible, but only on 2 conditions:
- The more powerful Perl regexp engine is used as UltraEdit regexp engine capabilities on multi-line replaces is limited.
- There are not too many bytes respectively lines between owner tag and the tags with first and last name.
I don't know how many bytes a Perl regular expression can really match at once. I have tried once to find that out, but have had various results on running same regular expression find on various files. However, up to 32 KB are no problem and most likely 64 KB work too. But if your files have several hundred KB or even MB and the number of bytes between
owner tag and the tags with first and last name can be much more than 64 KB, using a Perl regular expression Replace in Files is strongly not recommended by me as the result can be wrong.
But before I give you the solution how to achieve the task by opening one file after the other, get first and last name into string variables or a clipboard (better for Unicode characters) and paste them as value of the
owner tag, here is the solution for using a Perl regular expression Replace in Files.
- Code: Select all
UltraEdit.perlReOn();
UltraEdit.frInFiles.replace("(?s)(<owner>).*?(</owner>.+?<firstname>)(.+?)(</firstname>.*?<lastname>)(.+?)(</lastname>)", "\\1\\3 \\5\\2\\3\\4\\5\\6");
UltraEdit.ueReOn();
The explanation for
(?s) can be read on topic
"." in Perl regular expressions doesn't include CRLFs?Just
(...
) is in Unix/Perl syntax the same as
^(...
^) in UltraEdit syntax. The tagged parts of a found string are referenced with
\1,
\2, ... in Unix/Perl syntax in comparison to
^1,
^2 in UltraEdit syntax. Because the backslash character is an escape character in Javascript strings, it is necessary to escape every backslash in a Unix/Perl regular expression search/replace string in scripts with an additional backslash.
.* in Unix/Perl syntax is like
?++ in UE syntax - a greedy expression to match any characters except newline characters (or including newline characters if
(?s) also used) any number of times. A question mark must be appended to make the expression non greedy. So
.*? in Perl syntax is like
* in UE syntax. The legacy Unix engine does not support
? to make an expression non greedy.
The Unix/Perl equivalent for UE regexp
?+ to match 1 or more characters except newline characters is
.+ which can be in Perl only made also non greedy by appending a question mark. So Perl regexp
.+? matches 1 or more characters, but as less as possible to fulfill the expression. In UE syntax it is not possible that
?+ works non greedy.