Regex to find the Substring in a String and also it's preceding number

Hello,
I have an issue with the Regex to find the exact string from the given Input txt and also it’s number.

Note: I need these values in two different variables(one to find GROOT in the given string and the other one if for the number corresponding to it if found).

Example:

String to be found- strPetName - GROOT
Input String - in_StrInput -

"
1) I AM GROOT
2) GROOT
3) GROOT IS MY PET’S NAME
4) GROOT’S HAPPY
5) GROOT - 2

"

Expected outputs should be like below,

out_IntNum = 2 (this can be string as well, it does not matter)
boolPetNameFound = True

Unfortunately, I can not share xamls or Input texts examples, hence using my pet’s name as a reference :slight_smile:

Thanks in advance,
Happy coding

1 Like

Hello @manvi.muchakurthi,

Welcome!

image

(?<=GROOT\s.).[0-9]+

You can check the pattern here Regex Test

cheers

4 Likes

@manvi.muchakurthi Use 2 assign activities as below:

out_IntNum = System.Text.RegularExpressions.Regex.Match(in_StrInput,“\d”).Value.Trim
boolPetNameFound = System.Text.RegularExpressions.Regex.Match(in_StrInput,strPetName).Value.Trim.Length>0

3 Likes

Thanks Pradeep, however result that I am expecting is number 2 and boolean for Groot on the line 2

@Arpit_Kesharwani Thanks,

results are 1 and True.
Results should be 2 for out_IntNum as I just need the line item which is equal to strPetname.
In this case, it should be GROOT from option 2.

I do not want any other lines extracted.

Hi @manvi.muchakurthi

You would need to use your variable as part of the input pattern in the Matches Activity.

strPetName = GROOT

(?<=strPetName\s.\s)[0-9]+

image

You then need to use an IF condition.
IF the output from the Regex is blank then do xxxx, Else write down the output.

Please let me know if I have misunderstood.

1 Like

Well in that case you can do this

  • strPetName = Groot

  • use boolType = System.Text.RegularExpressions.Regex.IsMatch(in_StrInput,strPetName)

  • and to get Number matchVar = System.Text.RegularExpressions.Regex.Match(in_StrInput,“(?<=”+strPetName+“\s.).[0-9]+”)

Cheers
@manvi.muchakurthi

1 Like

@manvi.muchakurthi Can you check this Workflow and see if it is able to provide the expected outputs for all the input data that you have :
RegexFind.xaml (7.6 KB)

Thanks,

Can you help me delete this post? I know I can mark this as a solution but I never got a solution from this post. I just need to delete this post.

@manvi.muchakurthi I think only a moderator can delete the posts :sweat_smile: But If you have found a different solution, you can post that solution here, and mark it as a Solution. So that other people having the same kind of problem can benefit from your solution.

If you’re still seeking for a solution then there are people who can help, you just need to describe the problem clearly to make it more understandable why wasn’t the solutions that we already had provided was not working.

Hope this solution will help you
RegexFind.xaml (6.6 KB)

1 Like