Get Characters before and after a specific keyword in a notepad

Hi Team,

I have a keyword in a notepad file which is extracted as a variable. Suppose in the below document, I extracted the keyword “government”, and I save it into a string variable.

I need to get 100 characters before and 100 characters after this keyword which is saved as a variable.

What is the manipulation I can do in uipath to get the result?

Thank You.

Hi @Techno1

Could you share the sample input file and expected output?

Regards
Gokul

Hi @Techno1

Please try this

System.Text.RegularExpressions.Regex.Matches("Your string",".{0,100}government.{0,100}")

This is to match 0 to 100 characters before and after ‘government’ keyword

This will give you a matchcollection which can be accessed usign for loop and changing type argument to Match, Then currentitem.Value will give each matched value if multiple matches are found

cheers

Thank you for your reply,
I have the keyword “government”, which is saved as a variable. How to use this variable in the manipulation.

Hi @Techno1

How about this expression

System.Text.RegularExpressions.Regex.Matches(Yourstring,"\S.{0,100}(?=government)|(?<=government\s)\S.{0,100}")

You can give this expression in the For each activity and Use message box activity inside the For each to see the result.

Regards
Gokul

Hi @Techno1

give like this

System.Text.RegularExpressions.Regex.Matches("Your string",".{0,100}" + Variable + ".{0,100}")

Variable is where you will give the variable

cheers

What will be “Your String” value?

Here Yourstring will be the input @Techno1

Hi @Techno1

You string is where you will pass your input string read from notepad

Give inverted comma if you want to write the value else directly give the string variable

cheers

Thank you for the reply team,

I have one more doubt,
Suppose in the example that i have given above, I want to take the keyword “prosper” which is before a full stop in the second line. so when i run the bot, i’m not able to get the 100 characters after the full stop.
So i want to add a sentence “end of line” after the full stop (if the keyword is coming before a full stop).

Hi @Techno1

What do you mean you are not able to get? Is the Regex not extracting after full stop(.) ?

cheers

What I meant is, in the above example, suppose we have the keyword “labor”, There is a full stop (.) after the keyword and there is no more text after the keyword, which means it is the end of the sentence, and it won’t take the 100 characters after the keyword, since there is no more character.
So, my question is like how we can add some word like “/end of line/” at the end of the output if there is no more characters after the keyword, to let the user know that it is end of the sentence.
Possibly within the previous expression that you have given me.

Thanks.

@Techno1

So if I understand correctly …After identifying say there are less than 100 characters identified because of sentence being lesser than 100 characters then you want to add a word as end of line?

So this is applicable say even if you identify 99 characters you want to write end of line after that?

If that is the case then after extracting the data using provided regex

use a split and do count…if count is less than 100 append end of line

1.Extract regex amd say string is stored in str
2. in If condition str.tolower.Split(“government”)(1).count < 100
3. If yes str = str + “End of line” .

cheers

@Anil_G

In our scenario we have to print 100 characters before and 100 characters after the keyword.
But let’s suppose the keyword is the last word in the sentence and in the txt file. Like the keyword “labor” in this screenshot below.

So, it will print only the previous 100 characters and not the 100 characters after the keyword since it is the end of sentence in the text file and there is no characters to print after the keyword.
So, i want to print a sentence “End of line” after the keyword “labor”, if it is the last word of txt file.

Thanks.

@Techno1

These are the steps

System.Text.RegularExpressions.Regex.Matches("Your string","(\s|.){0,100}" + Variable + "(\s|.){0,100}")

1.Extract regex and say string is stored in str
str= System.Text.RegularExpressions.Regex.Matches(“Your string”,“(\s|.){0,100}” + Variable + “(\s|.){0,100}”)(0).Value
2. in If condition str.tolower.Split(variable)(1).count < 100
3. If yes str = str + “End of line” .

Of if condition is not count and you want to check only full stop in step 2 use below
str.tolower.Split(variable)(1).Trim.Equals(“.”)

variable - This is the split string

cheers