Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
How to capture the line before a regex match ?
-
I have the following text :
This is paragraph #1 [comment]: # (London, Paris) This is paragraph #2 [comment]: # (Paris, Berlin) This is paragraph #3 [comment]: # (London, Berlin) This is paragraph #4 [comment]: # (Paris) This is paragraph #5 [comment]: # (Berlin)
I want to match all the lines containing Berlin AND for each match, the line before, in order to fold all the lines NOT containing this pattern :
.....(folded)..... This is paragraph #2 [comment]: # (Paris, Berlin) This is paragraph #3 [comment]: # (London, Berlin) .....(folded)..... This is paragraph #5 [comment]: # (Berlin)
I came up with this solution
((.*\n){1}).*Berlin.* which works under Python in Regex101 and which you can find at [https://regex101.com/r/jXD7gw/3](link url) But when I try it in Editorial, it fails (simple workflow with FOLD LINES CONTAINING… with that regex and, of course the INVERT button on. Where did I go wrong ? Thanks in advance
-
I am not an Editorial user, and I do not understand where exactly you use this regexp, but adding one more backslash before the one you have there might help, in case the string gets evaluated as a string before it is used as a regexp.
-
Do you mean \\n ?
((.*\\n){1}).*Berlin.*
I tried it, but it does not work.
Thank you anyway
-
BTW, I also tried this simpler solution :
.*\n.*Berlin.*
which also works in Regex101, but, alas, not in the "Fold Lines Containing" action…
-
It seems like
\n
cannot be used properly in the "Fold Lines Containing" regex. For example, if you try to fold all lines containing\n
, nothing happens, i. e. the regex never matched. Since your regexes contain a\n
, they never match, so if you invert them the entire text gets folded.I've noticed that the
^
and$
characters (which stand for beginning/end of text) match the beginning and end of each line, so most likely Editorial tries to match the fold regex on each line separately, rather than the whole text at once. -
@dgelessus : Thank you for your answer. which is what I had (approximately) figured.
How is it possible to make to @olemoritz the suggestion/request/wish to "unleash" the power of regex in that specific action (and elsewhere, of course, if needed) ?
This would make the irreplaceable Editorial even better.
Thanks again