is regex condition possible in replace-string
-
I convert genealogic indexes from archive-sites into a ged-file, so they can be imported in my genealogic program.
In the searchstring I have made a named expression called (?<CTNOTE>…). In some cases it is possible that the CTNOTE expression is an empty string.
So I want to insert the lines 2 NOTE @N01@ and 0 @N10@ NOTE $+{CTNOTE} in the replaced result text only when the CTNOTE expression is not empty. Is it possible to write an replace-regex that does this job and how is it written down?
-
Again, this is more a RegEx related question.
There are several websites dedicated to RegEx, just google for it. -
Hi, @jos-maas and All
Sorry, I didn’t understand, exactly, what string must replace your matched search, but here is the general description of the back-references and conditional replacements syntaxes, in the REPLACE part, when using named groups :
SEARCH
(?-s)(?<jos>^\d+$)|.+REPLACE
(?{jos}Rewrite $+{jos}, as Group 'jos' exists:Group 'jos' does NOT exist !)With the reges S/R, above, the text :
123 This is a test 4567890would be changed into :
Rewrite 123, as Group 'jos' exists Group 'jos' does NOT exist ! Rewrite 4567890, as Group 'jos' exists
Notes :
-
As usual, the
(?-s)part is added to be sure that the dot symbol will match standard characters, only ! -
Then we’re searching, either :
-
A single integer number, per line, stored as group jos
(?<jos>^\d+$)( It’s the priority search, as the first branch of the alternative ) -
All NON-empty contents of current line
.+
-
-
In replacement, we use the conditional replacement
(?{jos}++++++++:--------), which means :-
If the group jos exists, we rewrite the ++++++++ text ( part before the colon symbol )
-
If the group jos does not exist, we rewrite the -------- text ( part after the colon symbol )
-
So :
-
If a line contains a single number, the group jos exists and we re-write
Rewrite $+{jos}, as Group 'jos' exists, with$+{jos, being a back-reference to the named group jos -
ELSE the group jos does not exist and we, simply, rewrite the literal text, located after the colon symbol
Group 'jos' does NOT exist !
I do hope that this simple example will help you to solve your problem, easily !
Cheers,
guy038
-
-
Bonjour, Guy038,
Your help was to the point, as showed by snippets from the search and replace string hereafter.
In the searchstring: (Opmerking\R+(?<CTOPM>[A-z ]*$)\R+) - there is no need to have an alternation operator and string, because there is a match if “Opmerking\R” is in the tekst and the CTOPM-named substring is not empty, or not.
The replacestrings: (?{CTOPM}2 NOTE @N10@\r\n:) and (?{CTOPM}0 @N10@ NOTE $+{CTOPM}\r\n:) generate the wanted lines if there is a match, or do nothing if there is no match.
This item and that in my previous post illustrate the problem that I have as a newby in regex matters. As I know from my studies - some fifty years ago - you need basic theory, advanced theory and applied theory books to learn a job. I feel there is a lack specific for the regex in notepad++. The tutorial could be seen as an applied handbook, your “SYNTAXE des expressions RÉGULIÈRES PRCE, de NOTEPAD++ v6.0 et PLUS” is perhaps advanced theory, but - as far as I can see - both don’t give a complete overview of all aspects of the regex in notepad++. So far your replies on my posts provide the needed information in addition.
I am grateful for your help on both my posts and I surely will write a call for help when I encounter a new problem.
Cordialement, Jos
-
Hi, @jos-maas,
Oh yes ! You’re perfectly right. Even, in the example, described in my previous post, we don’t need any alternative !
But, in that case, when a line does not match ( NO group jos ), this line stays unchanged, and we do not need, either, the ELSE part of the conditional replacement !
So, the previous regex S/R, becomes :
SEARCH
(?-s)(?<jos>^\d+$)REPLACE
?{jos}Rewrite $+{jos}, as Group 'jos' existsAnd, with the original text :
123 This is a test 4567890We would get, this time, the text :
Rewrite 123, as Group 'jos' exists This is a test Rewrite 4567890, as Group 'jos' existsNote that, in replacement, the outer parentheses are removed, without any problem !
Jos, for further information, about regular expressions concept and syntax, begin with that article, in N++ Wiki :
http://docs.notepad-plus-plus.org/index.php/Regular_Expressions
In addition, you’ll find good documentation, about the Boost C++ Regex library, v1.55.0 ( similar to the PERL Regular Common Expressions, v1.48.0 ), used by
Notepad++, since its6.0version, at the TWO addresses below :http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html
http://www.boost.org/doc/libs/1_48_0/libs/regex/doc/html/boost_regex/format/boost_format_syntax.html
-
The FIRST link explains the syntax, of regular expressions, in the SEARCH part
-
The SECOND link explains the syntax, of regular expressions, in the REPLACEMENT part
You may, also, look for valuable informations, on the sites, below :
http://www.regular-expressions.info ( The reference !! )
http://perldoc.perl.org/perlre.html
Be aware that, as any documentation, it may contain some errors ! Anyway, if you detected one, that’s good news : you’re improving ;-)
Cheers,
guy038
-
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login