User to user discussion and support for UltraEdit, UEStudio, UltraCompare, and other IDM applications.

Find, replace, find in files, replace in files, regular expressions
9 posts Page 1 of 1
I need to replace this line:

MOA***M15*M15*M15~

with:

MOA***M15~

How can I write a generic expression as M15 could change. It could be N121*N121*N121*N121 and so forth. The count could also change. For example it could be duplicated 2, 3, 4, or more times.

Thanks.
Try:

Find what: MOA\*{3}(([A-Z]\d+)\*?){1,}~
Replace with: MOA***\1~

Regular expressions - Perl

Attachments

It's impossible to lead us astray for we don't care even to choose the way.
Thanks for looking into it.

It finds all of these matches in my test file below but the only valid one is the last.

MOA***N657~
MOA***M15~
MOA***N657~
MOA***N657*M15~
MOA***N30~
MOA***N30~
MOA***N30~
MOA***M15*M15*M15~

Do you have any other suggestions?
Find what: MOA\*{3}(((M|N)\d+)\**){3,}~
It's impossible to lead us astray for we don't care even to choose the way.
Use as Perl regular expression search string (\*[MN]\d+)(?:\1)+~ and as replace string \1~

Explanation:

(...) ... a marking (capturing) group. The string found by this expression is back-referenced twice, one times with \1 in search string and one more times also with \1 in replace string.

Perl regular expression supports back-referencing in search and replace string while the UltraEdit and Unix regular expression engines support it only in replace string.

\* ... asterisk interpreted as literal character because of escaping it with a backslash.

[MN] ... either M or N specified in a character set; case depends on Match case option.

\d+ ... any digit 1 or more times.

(?:\1)+ ... a non marking group with an expression or fixed string which must be applied 1 or more times whereby the expression is in this case just the fixed string found already before with expression \*[MN]\d+ in the marking group.

~ ... simply this character.

Well, the non marking group is in this case not really needed. It just helps making it easier to read the expression.
The search string (\*[MN]\d+)\1+~ works, too.
Best regards from Austria
Ovg, some hints:

Never use a marking group within a marking group. The behavior on back-referencing is undefined in this case. 1 or more not nested marking groups within a non marking group is okay as also several even nested non marking groups within a marking group. But don't use a search string with a marking group within a marking group.

It can be used always + instead of {1,} independent on what is left the multiplier. Both are a greedy multiplier for 1 or more times.

It can be used always ? instead of {0,1} meaning 0 or 1 times. ? is often called optional multiplier as the string or expression left can optionally exist (one times), but must not exist at all.

Note: ? after a not escaped ( changes meaning of the group depending on the next character(s) from a marking group to a non marking group, a positive or negative lookahead or lookbehind or a pattern modifier.
Best regards from Austria
Use as Perl regular expression search string (\*[MN]\d+)(?:\1)+~ and as replace string \1~

Thank you very much that works. Your explanation will be beneficial to my future "attempts" at finding these complicated streams.

I did not know backreferencing using the Unix engine in Find did not work. Should I just be using Perl regex engine for the most part? It seems to be the most flexible.

Sitrucz
Mofi, thanks for your suggestions!
It's impossible to lead us astray for we don't care even to choose the way.
sitrucz wrote: Should I just be using Perl regex engine for the most part? It seems to be the most flexible.

The Perl regular expression engine is the most powerful. But this also means Perl regex syntax is the most difficult to learn. What to learn first depends on how often you need to do complex regex finds/replaces and how often simple regex finds/replaces are needed for a task. However, with Perl you can really do everything which can be done also with the other engines, with the exception of including selected text or text in clipboard in search/replace strings which is supported only on non regex or UltraEdit regex finds/replaces.
Best regards from Austria
9 posts Page 1 of 1