Regular Expression to Capture each line of 'anything', for as many lines available

Hi,

I'm using the Search Variable Using Regular Expression and while my current regex is 'working', it only works for Variables (text files) that have a specific number of lines, and I have some files that have less or more lines.

Basically I am importing a text file with multiple lines. I need to capture each individual line to a unique capture group which then will save 'capture groups to Variables in the Action.

Here's an examples of the Text File:

IP
Test
Open
Genre
00:49

Dm
112BPM

Some lines may be blank, and while this file has 8 lines, some may have only 6 lines.

This regular expression will work, but only for 8 lines. If I add another blank line it won't work. If I remove any single line, it won't work.

(.*)\R\h*(.*)\R\h*(.*)\R\h*(.*)\R\h*(.*)\R\h*(.*)\R\h*(.*)\R\h*(.*)

To be clear, this needs to have 8 total capture groups (for the KM Action), but it still needs to work even if there are 'less than 8 lines' in the text file.

Here's the action:

image

Any help is greatly appreciated!

Thanks!

1 Like

May I ask why you are using this approach, of saving every line to a KM Variable?

Without knowing your objective or complete workflow, it might make more sense to use a For Each action with a Lines collection from a file. Something like this:

image

Questions?

1 Like

OK, I think I misread you for my above reply. Try this RegEx:
(.*)\R\h*(.*)\R\h*(.*)\R\h*(.*)\R\h*(.*?)\R\h*(.*?)\R?\h*((?:.+)?)\R?\h*((?:.+)?)

For details, see: regex101: build, test, and debug regex

Example KM Search Actions

image

Let us know if this works for you.

1 Like

Hi, yeah I uploaded an image of the Action I'm using to my original post - basically what you just posted.

Your regex definitely works for 6-8 lines, but is there a way to modify it for anywhere between maybe 4 - 8 lines? It's possible that I won't ever have just 4 lines, but I'm just trying to cover my bases now just in case.

Thanks so much!

Should be. The key is the pattern I used for the last two lines:
\R?\h*((?:.+)?)

You should be able to use that for the preceding lines as well.
Test it out using regex101: build, test, and debug regex

Yeah it looks like this works (repeated multiple times):

\R?\h*((?:.+)?)

But on regex101.com it gives me a set of Matches - "Match 1" and then a set of blank matches "Match 2", any idea why?

Also, I can just repeat this 8 times, but isn't there a shorthand? I tried a few different things and it didn't work:

(\R?\h*((?:.+)?)){8}

Thanks again for your help!

I don't know if this will work -- you'll just have to experiment! :wink:

{8} means exactly 8 times.

The syntax is:
{min,max}, so you might try:
{1,8}

Haven't tried that with optional CGs, so I don't know if it will work or not.

If you find a solution, please post back here so all can benefit.

1 Like
\R?\h*((?:.+)?)

This works but I have made some changes and have a new issue. I changed the search data and I only want to capture the data after the colon + space, so for Volume: I just want '200'. I have tried modifying the regex and I just can't seem to crack this one and still keep the number of lines variable. Any ideas?

See: https://regex101.com/r/7NmClN/1

You might try this. I only gave it very limited testing, but it seems to work when tested at the end:
\R?\h*(?:\w+\: )?((?:.+)?)

THANK YOU so much! This works, I just added one 'period' for when the Heading is two words like 'Music Reference: ' etc.

\R?\h*(?:\w.+\: )?((?:.+)?)

Much appreciated! I worked on this for hours over multiple days and could not crack it!