Skip to content

Regex Replace AND Match in One #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Alexanders RegEx (independent Publisher)/ReadMe.md
Original file line number Diff line number Diff line change
@@ -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.
67 changes: 67 additions & 0 deletions Alexanders RegEx (independent Publisher)/RegEx_Code.cs
Original file line number Diff line number Diff line change
@@ -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<HttpResponseMessage> 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<HttpResponseMessage> 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<HttpResponseMessage> 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;
}

}
75 changes: 75 additions & 0 deletions Alexanders RegEx (independent Publisher)/RegEx_Swagger.yml
Original file line number Diff line number Diff line change
@@ -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: []