Find-Replace. Can NPP "remember" the character and transfer it to the output?
-
Fellow Notepad++ Users,
Could you please help me with the following search-and-replace problem I am having?
I understand in the Find-Replace menu that one can use ‘.’
to represent any character. However can NPP “remember” the character and transfer it to the output?if I find
[3]
then replace withconsec(3)
Here is the data I currently have (“before” data):
[3],[5],[4],[4],[3],[4],[3],[5],[2],[5],[2],[3],[2],[1],[5],[3],[5],[5],[2],[3],[1],[1],[3],[4],[5],[1],[3],[4],[3],[5],[2],[5],[4],[4],[5],[3],[1],[5],[1],[1],[4],[1],[5],[4],[3],[2],[3],[1],[2],[5]
Here is how I would like that data to look (“after” data):
System.out.println(dataStream.consec(3)); System.out.println(dataStream.consec(5)); System.out.println(dataStream.consec(4)); System.out.println(dataStream.consec(4)); System.out.println(dataStream.consec(3)); System.out.println(dataStream.consec(4)); System.out.println(dataStream.consec(3)); System.out.println(dataStream.consec(5)); System.out.println(dataStream.consec(2)); System.out.println(dataStream.consec(5)); ....and so on...
-
@Anil-Philip said in Find-Replace. Can NPP “remember” the character and transfer it to the output?:
[1],[5],[3],[5],[5],[2],[3],[1],[1],[3],[4],[5],[1],[3],[4],[3],[5],[2],[5],[4],[4],[5],[3],[1],[5],[1],[1],[4],[1],[5],[4],[3],[2],[3],[1],[2],[5]
This works for me:
Ctrl+H (Replace):
Find What: \\[([0-9])\\],?
Replace With: System.out.println\(dataStream.consec\(\1\)\);\n
Regular expression: checked
Wrap around: checked
Replace All\1 remembers the data of the first pair of parentheses
-
This post is deleted! -
@datatraveller1 Thank you! Can you please clarify what the
?
does? Over here it says: Matches the previous element zero or one time. -
@Anil-Philip Yes, it means that the number in square brackets may or may not be followed by a comma, the last one is not:
… [1],[2],[5] -
Hi @guy038 and all: Is it possible to just search for the square brackets with numbers in them (without the comma part) and create the result string from that? While my solution works, I have the impression there might be a more elegant solution.
-
Hello, @datatraveller1 and All,
No, I think that your regex S/R is just the right one and cannot be improved significantly ;-))
Perhaps it’s good to recall that, at the end of your replacement string, you must insert
\r\n
if you use Windows files OR\n
if you use Unix files
An other formulation would be to split out this S/R in two individual S/Rs :
-
SEARCH
[([0-9])],?
-
REPLACE
$1\)\);\r\n
Then :
-
SEARCH
(?-s)^(?=.)
-
REPLACE
System.out.println\(dataStream.consec\
But, no advantage for doing this as the first search regex is identical in the two cases !
Best Regards,
guy038
-