How to use regular expression to capture content?

Hi

I need to capture content in a text file, start from “SRF_320_0”, end with “SRF_330_0”.
All data in between this two are needed, but my pattern shows a mistake as follows:
image

Thanks
Jing

i have tried
/SRF_320_0(.\n)+(?=SRF_330_0)/
SRF_320_0(.
\n)+(?=SRF_330_0)

all cannot work…

@jingwang0222 Try like below

   "(?<=SRF_320_0).+(?=SRF_330_0)"
1 Like

It may work! thanks ! But i cannot test if it capture the correct content.
I use .tostring and want to input it in the text file, but text file only show
System.Linq.Enumerable+d__95`1[System.Text.RegularExpressions.Match]

any idea how to change this to string?

@jingwang0222 no idea bro

thanks

Hey @jingwang0222

You can use For each activity . In Values option you put the Collection of Matches. And change the For each Type Argument Property to System.Text.RegularExpressions.Match

Regards…!!
Aksh

1 Like

@jingwang0222
Regex to retrieve data from multiple lines is little tricky.
If you know the keys that you need to search, use simple string manipulation here

  1. Find the index of starting match text, “SRF_320_0” => startIndex = strContent.IndexOf(“SRF_320_0”)
  2. Check for existence of this key, startIndex > -1
  3. Find the index of ending match text, “SRF_330_0” => endIndex = strContent.IndexOf(“SRF_330_0”)
  4. Check for existence of this key, endIndex > -1
  5. Find length of the string located in between starting text and ending text, matchContentLength = endIndex - startIndex
  6. Get the content that you are looking into, strMatchContent = strContent.SubString(startIndex, matchContentLength-1)

Hope this helps!

1 Like

Hi aksh
I have changed to that type, but still cannot get a string output, thanks

Hey @jingwang0222

You have to get the groups and then access them as like i did in that image example :slight_smile:

in your case you can access your group values by index like Groups(0) and so…
Let me know if still you are not able to figure it out.

Regards…!!
Aksh

Hi there, I’ve been following this thread to also get a similar regex to a string so that I can use it in a write range in excel. I keep getting the “system.Linq.enumerable+d…” (like the previous user mentioned) in the cells instead of the string. I don’t think I am understanding your image or how to assign the groups? Could you send a xaml of possible? Thank you, I am really new to regex and it’s a bit mind boggling!

hi

Thanks, but my workflow is a bit complicated even my input file is confidential that i cannot send it as well, and I already solve it by using split keywords rather than regex.

Thanks again.

Here is a detailed article on that :slight_smile:

Hi, much like yourself i had a similar issue whilst attempting to pull out the match as a string. I’m sure you’ve probably figured it out, but for anyone else:

My solution in the end was to:
Initially get the variable i wanted to check over - strCheckVal

Use an isMatch activity & within the properties. This outputs a boolean value:
Provide your input string (the string you want to analyse (in my case strCheckVal), within Pattern input your regular expression (surrounded with quotation marks) & create the output variable e.g. blnIsFound

After this drag over an IF statement with the following condition:
blnIsFound = True

Within the ‘then’ side:
Drag over a ‘Matches’ activity. This outputs a collection (different from isMatch).
Within the properties of the ‘Matches’ activity, again put in the same input string, the same pattern but within the ‘Results’ property create a new variable e.g Coll_Of_Matches

Underneath this (but still within the 'if > then section) add a 'For Each; activity (not the one for datatables, the other one).
Within the for each activity, populate as follows:
For each ‘item’ in Coll_Of_Matches (Type Argument = Object within the properties)
And within the body, drag over a ‘write line’ activity with the text like “Match found”+item.ToString

If you want to assign it to a variable, simply drag over an assign activity within the body of the For Each and have the left side what variable you want to assign it to & the right side item.toString

*Remember to change your scope of your newly assigned variable so you can use it in other areas of your code, if need be.

**Also, if you chose not to have the IF statement, you run the risk of getting an error associated with the variable being empty (e.g. no match to your regular expression) and nobody wants that hassle…

Hope this helps someone as it took me longer than i’d care to mention to figure it out.