From 3d4b72af4408fb66fcd20f07cae05d695e14610e Mon Sep 17 00:00:00 2001 From: Alexander Worley Date: Wed, 31 Aug 2022 14:30:48 -0400 Subject: [PATCH] Regex Replace AND Match in One I updated and combined both Replace and Match into a single c# file and swagger. I wish to append this to this great piece of work as the other options in Power Automate are very sparse and often behind paywalls. I tested it and confirmed everything works as expected. --- .../ReadMe.md | 4 + .../RegEx_Code.cs | 67 +++++++++++++++++ .../RegEx_Swagger.yml | 75 +++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 Alexanders RegEx (independent Publisher)/ReadMe.md create mode 100644 Alexanders RegEx (independent Publisher)/RegEx_Code.cs create mode 100644 Alexanders RegEx (independent Publisher)/RegEx_Swagger.yml diff --git a/Alexanders RegEx (independent Publisher)/ReadMe.md b/Alexanders RegEx (independent Publisher)/ReadMe.md new file mode 100644 index 0000000..4296f54 --- /dev/null +++ b/Alexanders RegEx (independent Publisher)/ReadMe.md @@ -0,0 +1,4 @@ +# Alexander's RegEx Replace & Match +I updated and combined both Replace and Match into a single c# file and swagger. +I wish to append this to this great piece of work as the other options in Power Automate are very sparse and often behind paywalls. +I tested it and confirmed everything works as expected. \ No newline at end of file diff --git a/Alexanders RegEx (independent Publisher)/RegEx_Code.cs b/Alexanders RegEx (independent Publisher)/RegEx_Code.cs new file mode 100644 index 0000000..d959948 --- /dev/null +++ b/Alexanders RegEx (independent Publisher)/RegEx_Code.cs @@ -0,0 +1,67 @@ +using System.Threading.Tasks; +using System.Net.Http; +using Newtonsoft.Json.Linq; +using System.Text.RegularExpressions; +using System.Net; + +public class Script : ScriptBase +{ + public override async Task ExecuteAsync() + { + if (this.Context.OperationId == "RegexReplace") + { + return await this.PerformRegexReplace().ConfigureAwait(false); + }else if(this.Context.OperationId == "RegexMatch") { + return await this.PerformRegexMatch().ConfigureAwait(false); + } + + HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.BadRequest); + response.Content = CreateJsonContent($"Unknown operation ID '{this.Context.OperationId}'"); + return response; + } +private async Task PerformRegexReplace() + { + // Manipulate the request data as applicable before setting it back + var requestContentAsString = await this.Context.Request.Content.ReadAsStringAsync().ConfigureAwait(false); + var requestContentAsJson = JObject.Parse(requestContentAsString); + + var input = (string)requestContentAsJson["input"]; + var pattern = (string)requestContentAsJson["pattern"]; + var replacement = (string)requestContentAsJson["replacement"]; + + var regexResult = Regex.Replace(input, pattern, replacement); + + // Manipulate the response data as applicable before returning it + JObject output = new JObject + { + ["output"] = regexResult + }; + + var response = new HttpResponseMessage(HttpStatusCode.OK); + response.Content = CreateJsonContent(output.ToString()); + return response; + } + private async Task PerformRegexMatch() + { + // Manipulate the request data as applicable before setting it back + var requestContentAsString = await this.Context.Request.Content.ReadAsStringAsync().ConfigureAwait(false); + var requestContentAsJson = JObject.Parse(requestContentAsString); + + var input = (string)requestContentAsJson["input"]; + var pattern = (string)requestContentAsJson["pattern"]; + + + var regexResult = Regex.Match(input, pattern).Value; + + // Manipulate the response data as applicable before returning it + JObject output = new JObject + { + ["output"] = regexResult + }; + + var response = new HttpResponseMessage(HttpStatusCode.OK); + response.Content = CreateJsonContent(output.ToString()); + return response; + } + +} \ No newline at end of file diff --git a/Alexanders RegEx (independent Publisher)/RegEx_Swagger.yml b/Alexanders RegEx (independent Publisher)/RegEx_Swagger.yml new file mode 100644 index 0000000..a5b1162 --- /dev/null +++ b/Alexanders RegEx (independent Publisher)/RegEx_Swagger.yml @@ -0,0 +1,75 @@ +swagger: '2.0' +info: + title: RegEx + description: Regex match and replace commands built into Power Automate directly. + version: '1.0' +host: api.contoso.com +basePath: / +schemes: + - https +consumes: [] +produces: [] +paths: + /regexReplace: + post: + responses: + default: + description: default + schema: + type: object + properties: + output: + type: string + description: output + summary: Alexander's RegEx Replace + description: RegEx replace built into Power Automate + operationId: RegexReplace + parameters: + - name: body + in: body + required: false + schema: + type: object + properties: + input: + type: string + description: input + pattern: + type: string + description: pattern + replacement: + type: string + description: replacement + /regexMatch: + post: + responses: + default: + description: default + schema: + type: object + properties: + output: + type: string + description: output + summary: Alexander's RegEx Match + description: RegEx Match built into Power Automate + operationId: RegexMatch + parameters: + - name: body + in: body + required: false + schema: + type: object + properties: + input: + type: string + description: input + pattern: + type: string + description: pattern +definitions: {} +parameters: {} +responses: {} +securityDefinitions: {} +security: [] +tags: []