Regex to find the second occurrence of a word

Hi Team,

Consider a string like this:
“name: XXXXXXXXX
address: XXXXXXXXX
pincode: XXXXXXXX
name:XXXXXXXXX
address: ABCDEF
pincode: XXXXX”

from this string i want to extract the data between second occurrence of name and address. i.e., “ABCDEF” .

1 Like

Hi @jishnupnair1996,
You can just use this regex expression:

And point to second group like regex_output(0).groups(1)

hi @jishnupnair1996
I’ll suggest you to follow these steps:.

  1. Read text file and store your string in a String Variable —> Text
  2. Use Matches activity and set pattern as (?>address: )\w* —> so it’ll take text after every address: (with address:)
    Output Variable—> Address
  3. Assign to get 2nd occurrence
    str1 (String) = ((Address(1).toString).split({“:”},StringSplitOptions.None)(1)).trim
    Address(1) —> to get 2nd occurrence from collection (Address)
    .split({“:”},StringSplitOptions.None)(1) —> to split with “:” and get text after “:”

I’m attaching my sample workflow for your understanding : RegexOccurence.zip (11.1 KB)

text Result—> res

I hope this will help you. :slight_smile:

2 Likes

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