LINQ and Regex, match string and enclose with string a text file

Hi friends,

I’m having problem with running through a text file line by line to look for a string match using Regex and putting prefix and suffix strings on that matched value and save it again. Thank you in advance.

Hi @iamthejuan

Can you share some input example and the expected output for the same ??

so that it will be easy for us to give you perfect solution

Happy Automation :raised_hands:

Best Regards
Er Pratik Wavhal :robot::man_technologist:t4: :computer:

please share Sample data, expected output and description of the requirements with us

<Test>UiPath.Excel</Test>
      <AssemblyReference>UiPath.System.Activities.Design</AssemblyReference>
      <AssemblyReference>System.Collections.Immutable</AssemblyReference>
      <AssemblyReference>System.ValueTuple</AssemblyReference>
      <AssemblyReference>UiPath.DocumentUnderstanding.ML.Activities</AssemblyReference>
      <AssemblyReference>UiPath.IntelligentOCR</AssemblyReference>
      <Test>UiPath.Mail</Test>
      <AssemblyReference>UiPath.OmniPage.Activities.Design</AssemblyReference>
      <AssemblyReference>UiPath.CV</AssemblyReference>
      <AssemblyReference>System.ServiceModel</AssemblyReference>

This is just a random string, in this example I want to add a parenthesis on the value between the and strings. In this examples the resulting string will become. Notice UiPath.Excel and UiPath.Mail is now enclosed with parenthesis.

<Test>(UiPath.Excel)</Test>
      <AssemblyReference>UiPath.System.Activities.Design</AssemblyReference>
      <AssemblyReference>System.Collections.Immutable</AssemblyReference>
      <AssemblyReference>System.ValueTuple</AssemblyReference>
      <AssemblyReference>UiPath.DocumentUnderstanding.ML.Activities</AssemblyReference>
      <AssemblyReference>UiPath.IntelligentOCR</AssemblyReference>
      <Test>(UiPath.Mail)</Test>
      <AssemblyReference>UiPath.OmniPage.Activities.Design</AssemblyReference>
      <AssemblyReference>UiPath.CV</AssemblyReference>
      <AssemblyReference>System.ServiceModel</AssemblyReference>

Thank you for a quick response. @Pratik_Wavhal @ppr

Hi @iamthejuan

So as you provided the random string on that basis below regex will work :-

And then using Replace Activity of Regex you can add parenthesis to that Extracted string

Hope this will be the output you are expecting

Mark as solution and like it if this helps you :slight_smile:

Happy Automation :raised_hands:

Best Regards
Er Pratik Wavhal :robot::man_technologist:t4: :computer:

3 Likes

@iamthejuan You can also give a Try on this :

System.Text.RegularExpressions.Regex.Replace(strInput,β€œ(<Test>)(.*)(</Test>)”,β€œ$1($2)$3”)

Where strInput is your Text Data and (<Test>)(.*)(</Test>) is the Pattern to be used, β€œ$1($2)$3” This Performs a Replace based on the Groups Captured.

1 Like

Hi @Pratik_Wavhal,

Thank you for the help, I was able to do your recommendation but for unknown reason it won’t save the string within the for loop activity. I will be creating another topic for that. Thank you for your help @Pratik_Wavhal. :slightly_smiling_face:

Thank you @supermanPunch, this works for me as I’m having issues using for loop.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.