Search code examples
regexfindsubstringfindallkate

Find all patterns matching regular expression using the Kate editor


I frequently use the Kate editor. It is possible to find patterns using regular expressions in Kate. For example the expression \d{3} will find all occurrences of 3 consecutive digits in the following text:

asdfsdf 234 dffd 234 f
d 182 sdsdas 182 sfdds
fdsfk 324 d 890 dfsdfd

Is it possible to extract the matching strings somehow? Or simply said: How do I get the following numbers only by using Kate? Is that possible in a simple way or possibly with a plugin? I know that I could for example use pythons re.findall() method but this is not what I want.

234
234
182
182
324
890

Solution

  • My 2-stepapproach consists in 2 steps:

    • Add a newline after 3 digits with (\d{3}) --> \1\n replacement (where \1 refers to the captured value) and then

    • Remove any character that is not a starting point for three digits and reinserting 3 digits captured with a capturing group with (\d{3})|. --> \1 replacement.

    The (\d{3})|. pattern matches and captures 3 digits OR just matches any character other than a newline, and \1 is a backreference to the value captured with (\d{3}).

    There may appear some empty lines, but they can usually be removed with built-in features.