Find a text in multiline and replace text
-
I would like to find a block of text which can be multi-line or single line.
- It will start with find first
- it will end with dot/period.
- Would like to replace “find first” with " for first "
- period should be replaced with " : end . "
- if there is any no-error within the text it should get removed.
Example 1.
find first customer no-lock where custnum = “Number1” no-error.
output should be
For first customer no-lock where custnum = “Number1” : END .Example 2
find first customer no-lock
where custnum = “Number1”
and Col1 = Val1
and col2 = val2
and col3 = val3
no-error.
output should be
For first customer no-lock
where custnum = “Number1”
and Col1 = Val1
and col2 = val2
and col3 = val3
: END. -
Hello, @shashi-bhosale and All,
Very easy with regexes !
-
Open the Replace dialog (
Ctrl + H
)-
SEARCH
(?s-i)find (?=first)(.+?)(?:no-error)?(?=\.$)
-
REPLACE
For \1: END
-
Tick, preferably, the
Wrap around
option -
Select the
Regular Expression
search mode -
Click on the
Replace All
button
-
Voila !
Notes :
-
First, the part
(?s-i)
ensures that :-
The dot regex char (
.
) matches any character, even EOL ones (\r
and\n
), due to the(?s)
syntax -
The search is processed in a sensitive way ( non-insensitive way ), due the the
(?-i)
syntax
-
-
Then, the part
find (?=first)
searches for thefind
string, with this exact case, followed with a space char -
But, ONLY IF it is followed with the
first
string, with this exact case, due to the look-ahead structure(?=first)
-
Then the middle part
(.+?)
matches the smallest non-null multi-lines range of any character, from after the stringfind
and the space char, stored as group1
, due to parentheses, till… -
… An optional string
no-error
, with this exact case, stored in a non-capturing group and ONLY IF it is followed with an literal period char, ending the current line, due to the final syntax(?:no-error)?(?=\.$)
-
In replacement, we rewrite :
-
First the string For, followed with a space char
-
Then, the contents of group
1
(\1
) -
Finally, the literal string
: END
, with this exact case !
-
Best Regards
guy038
-
-
good except if there is spaces after no-error it doesnt work .
find first ki.timeatt no-LOCK WHERE ki.TimeAtt.emp-num = ki.pcn.emp-num AND ki.TimeAtt.typecode = ki.pcn.typecode AND ki.TimeAtt.cdate = LvPCNDate NO-ERROR .
I want to ignore spaces or black new line and eliminate NO-ERROR .
-
Nothing in the whole block is case sensitive. Also i string could look like below too.
find first
ki.timeatt no-LOCK
WHERE
ki.TimeAtt.emp-num = ki.pcn.emp-num AND
ki.TimeAtt.typecode = ki.pcn.typecode AND
ki.TimeAtt.cdate = LvPCNDate
NO-ERROR . -
Hi, @shashi-bhosale and All,
Sorry for my late reply :-((
So, for instance given this INPUT text :
find first ki.timeatt no-LOCK WHERE ki.TimeAtt.emp-num = ki.pcn.emp-num AND ki.TimeAtt.typecode = ki.pcn.typecode AND ki.TimeAtt.cdate = LvPCNDate NO-ERROR . find first ki.timeatt no-LOCK WHERE ki.TimeAtt.emp-num = ki.pcn.emp-num AND ki.TimeAtt.typecode = ki.pcn.typecode AND ki.TimeAtt.cdate = LvPCNDate NO-ERROR.
The regex S/R, below :
-
SEARCH
(?si)find\x20+(first.+?)no-error\x20*\.\x20*$
-
REPLACE
For\x20\1:\x20END.
should give the expected OUTPUT text :
For first ki.timeatt no-LOCK WHERE ki.TimeAtt.emp-num = ki.pcn.emp-num AND ki.TimeAtt.typecode = ki.pcn.typecode AND ki.TimeAtt.cdate = LvPCNDate : END. For first ki.timeatt no-LOCK WHERE ki.TimeAtt.emp-num = ki.pcn.emp-num AND ki.TimeAtt.typecode = ki.pcn.typecode AND ki.TimeAtt.cdate = LvPCNDate : END.
As you can see, I changed and simplified the global logic of the S/R :
-
The words/string
find
,first
andno-error
are searched whatever their case, due to the initial syntax(?i)
. So, for instance, the wordsFind
,fIRsT
andno-ERRORS
will be matched ! -
The
\x20+
syntax, between the two wordsfind
andfirst
, represents any non-null range of space characters -
The
(first.+?)
searches the smallest range of chars, beginning from word first, whatever its case, till the next stringno-error
, excluded and is stored as group1
, due to the parentheses -
Then, the part
no-error\x20*
looks for the stringno-error
, whatever its case, followed with an optional range of space characters -
and the final part
\.\x20$*
searches for a literal dot char followed by optional space characters again, ending the current line -
In replacement :
-
The part
For\x20
replaces the wordfind
followed with space char(s) by the word For, followed with a single space char -
Then, the group
1
is rewritten (\1
) -
Finally, the part
:\x20END.
replaces the stringno-error
with optional space chars and a final.
char by the string: END.
with this exact case
-
Cheers
guy038
-
-
@guy038 said in Find a text in multiline and replace text:
For\x20\1:\x20END.
Thanks. This is great. Works fine.
I would like to change this to a choice values.Find first phila.employee no-lock no-error.
Find first oshk.employee no-lock no-error.
Find first Germany.employee no-lock no-error.The search should find phila and oshk but ignore anything which is not Phila or Oshk.
it should find first 2 lines and ignore 3rd line. -
Hello, @shashi-bhosale and All,
OK. So the new S/R is, simply :
-
SEARCH
(?si)find\x20+(first\x20(?:phila|oshk).+?)no-error\x20*\.\x20*$
-
REPLACE
For\x20\1:\x20END.
Notes :
-
I’ve just changed the part
(first.+?)
, which represents the group1
, by the syntax(first\x20(?:phila|oshk).+?)
-
As you can see, *after the string
first
, whatever its case, I inserted the regex\x20(?:phila|oshk)
containing, after the space char, a(?:••••••••)
structure, known as a non-capturing group. -
Inside this non-capturing group, it looks, either, for the string
phila
OR the stringoshk
, due to the alternation symbol|
-
Note that you may extend this list of alternatives, for instance, with the regex
(?:phila|oshk|third_case|fourth_case|fifth_case...)
Cheers,
guy038
-
-
Thanks a lot for help. What website you use to get explanation of the command ?
-
@shashi-bhosale said in Find a text in multiline and replace text:
Thanks a lot for help. What website you use to get explanation of the command ?
You could look in this forum’s FAQ section and find the FAQ entry about Where to find regular expression documentation. It links to the official Notepad++ regular expression documentation, as well as several external sites which are useful for learning about or experimenting with or describing the specifics of regular expressions.