Regex for specific pattern containing multiline text

Hi Team,

Need help in creating Regex for below -

Input -
Steps_taken “” loop
{
_1 “1st”
[
Subcat = “_1,_2,_3,_4,_5,_6,_7”
],
_2 “2nd”
[
Subcat = “_1,_2,_3,_4,_5,_6,_7,_98”
]
} fields -
(
slice "What were the steps you have taken from the time when the symptom appeared to when the pain was addressed? Select the steps in the sequence that you took the action.

You may repeat each step more than once but need not fill up all 10 steps.
{@}"
[
metatype = “rowpicker”,
row$all$center = true
]
categorical [1…1]
{
List_BroadstepsQ33 use \.List_BroadstepsQ33 -,
_98 “That’s all”
[
CAT = “_2,_3,_4,_5,_6,_7,_8,_9,_10”,
value = 98
]
fix exclusive
} ran;

) expand grid;

Output -
slice "What were the steps you have taken from the time when the symptom appeared to when the pain was addressed? Select the steps in the sequence that you took the action.

You may repeat each step more than once but need not fill up all 10 steps.
{@}"
[
metatype = “rowpicker”,
row$all$center = true
]
categorical [1…1]
{
List_BroadstepsQ33 use \.List_BroadstepsQ33 -,
_98 “That’s all”
[
CAT = “_2,_3,_4,_5,_6,_7,_8,_9,_10”,
value = 98
]
fix exclusive
} ran;

There can be multiple instances of such pattern in a text file.
I need to delete 2 part here -

  1. From the beginning of the line containing keyword ‘loop’ till keyword ‘fields -’
  2. Opening bracket (which will always be there below the line containing ‘fields -’) and closing bracket (which will always contain keyword ‘grid;’)

Test with this pattern:

(?<=fields -\r?\n\()[\s\S]+?(?=\) \w+ grid;)

1 Like

@ptrobot Thanks for your response.
I tried with the pattern you provided in ‘https://www.regextester.com/’ and it is selecting the text which I want to keep. Since there will be multiple occurrences of such pattern it will be helpful if you can provide regex that will help to replace rest part of the string which it is selecting now(since those keywords will be fixed in all patterns, like - loop, fields -, grid; and opening and closing brackets after ‘fields -’) .

Also the same pattern I tried in ‘https://regex101.com/’, but it is not working there instead returning as ‘pattern error’ (not sure why!!).

1 Like

regex101.com is not 100% compatible with UiPath/.Net Regex. Use .NET Regex Tester - Regex Storm instead.

Regular expressions is for extracting what you want to keep, not what you don’t want to keep. If you use the Matches activity, you can get all the matches in a collection. E.g. if there are two matches, you can get the second value with resultMatches(1).ToString.

2 Likes

Hey @DewanjeeS

Further to @ptrobot’s post - please check section 4 of my Regex Megapost for information on groups.

You can also use a “For Each” to action each result.

4 Likes