What is the best approach for creating Regex for getting theword before matching pattern?

Input : this is test=“[sample]” string with=“[some]” special words. “[another one]”

Expected out :

test
with

Pattern :
=[“Word”]

@Hari I hope, This will solve your problem, you need to learn the regex pattern then you are able to write the custom regex

UiPath Analysis

While UiPath doesn’t have built-in regex functionalities, you can leverage its regular expression activities within workflows for text manipulation tasks. Here’s a general approach:

  1. Import the System.Text.RegularExpressions Namespace:

    • In your UiPath project, use the Import Namespace activity to include System.Text.RegularExpressions. This grants access to regex classes.
  2. Create a Regular Expression Pattern:

    • Design the pattern to capture the word preceding the matching pattern (=\[([^\]]*)\]). Here’s a breakdown:
      • =: Matches a literal equal sign.
      • \[: Matches a literal square bracket.
      • (: Starts a capturing group.
      • [^\]]*: Matches zero or more characters that are not square brackets ([]). This captures the word before the brackets.
      • ): Ends the capturing group.
      • \]: Matches a literal closing square bracket.
  3. Use the Matches Activity:

    • Employ the Matches activity to find all occurrences of the pattern in your input string. Provide the input string and the regex pattern as arguments.
  4. Access Captured Groups:

    • The Matches activity outputs a collection of Match objects. To extract the captured word, use the Groups property of a Match object and access group 1 (index 0).

Tutorial Mode

1. Sample Input String:

"this is test=[sample] string with=[some] special words. [another one]"

2. UiPath Workflow:

  • Create a new UiPath workflow.

  • Drag and drop the following activities:

    • Assign (2 instances)
    • Matches
  • Variable Setup (Assign Activity 1):

    • Create two string variables:
      • inputString (to hold the sample input)
      • pattern (to store the regex pattern)
    • Assign the sample input string to inputString.
    • Assign the regex pattern =\[([^\]]*)\] to pattern.
  • Matching Words (Matches Activity):

    • Set the Input property to inputString.
    • Set the Pattern property to pattern.
    • Store the output collection of Match objects in a variable named matches.
  • Extracting Words (Assign Activity 2):

    • Use a loop to iterate through the matches collection.
      • Inside the loop, use another Assign activity:
        • Set the To property to a new string variable (e.g., extractedWord).
        • In the Value property, use the expression matches.Current.Groups(1).Value. This extracts the captured word (group 1) from the current Match object.
        • Print the extracted word to the output panel using a Write Line activity within the loop.

Expected Output:

test
with

Explanation:

  • The Matches activity finds all occurrences of the regex pattern in the input string.
  • The loop iterates through each Match object.
  • Within the loop, we access the captured word (group 1) using matches.Current.Groups(1).Value.
  • The extracted word is then printed to the output panel.

Additional Considerations:

  • Adjust the regex pattern if your word definitions or delimiters differ.
  • Consider using error handling to gracefully handle cases where the pattern might not be found.
  • Explore combining regex with other UiPath activities for more complex text manipulation tasks.
1 Like

hi @Hari i hope this is what you’re looking for.

(?<=[a-zA-Z]+=“[)[a-zA-Z]+(?=]”)

(?<=[a-zA-Z]+=“[) = starts with sth. like xxx=”[ or aA="[ etc.

[a-zA-Z]+ = at least one letter

(?=]“) = ends with ]”

cheers

1 Like

@Hari is your issue solved?

Thanks for the information , with some modification it worked. Cheers!

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