Using Regex matches in Microflow

0
Hi all,   In the string functions we have IsMatch for evaluating regexes against a string and retrieving a boolean. However, now I have a use case in which I need the match value itself. Is there a way to apply this in Mendix without building a java action? Is there a proven marketplace module which can help?   Given string: M 20-40 55-70 G-12 K   I want to test if the string has a number in it. That is possible with the IsMatch function. However, I want to retrieve the first number out of the string, thus 20, 55, 12 or empty.   Thanks for your help. Peter
asked
3 answers
1

All honour to Robert Price for his excellent blog article: http://www.robertprice.co.uk/robblog/mendix-regexreplaceall-example/

 

Community Commons RegexReplaceAll function has an hidden feature. You can use the $1 replacement as a way to use the matched item in the regex. By defining the whole string in your needle (thus from ^ to $), you are able to replace the initial string with the value between the ().

 

afbeelding.png

 

answered
0

Hi Peter,

 

Sorry I couldn't find a solution for this either and had to make a custom java action. I know you specifically asked not for one, but it's really easy to implement

 

Inputs:

InputString - string

Regex - string

 

Return string

 

at the start import:

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 

image.png

 

Really small and simple, I'm surprised Mendix hasn't added this into Community Commons yet

 

 

answered
0

Hey Peter,

I assume you solved the first section where you needed to match a string with the IsMatch function, and you want to stick only with Mendix to validate your number. For that, you can use the Split iterator from CommunityCommons (function: https://docs.mendix.com/appstore/modules/community-commons-function-library/#39-stringutils). First, you can use the Find function to find the first number in your string. Then, you can substring your string to become something like "55-70". When you get that string, you want to use the Split iteration function to get a list. The list will look like this: it will return a list of SplitItem objects. The first one will be Index-0 Value 50, and the second one will be Index-1 Value-70. You can just use the list operation head to get the first one, which will be 50. It's not the clearest solution, but if your requirement is to only stick with Mendix, this will work as expected. (Another solution would be to find the first character, then substring it to +1 if you will only have numbers from 00 to 99).

Best Regards, Slavko

answered