Split words

Hi Team,

I have a line like
This is Testing - 123 - 456

I want to print only - This is Testing
In other words i want only those words that are falling before the first hyphen.
Help needed

Hi @Balan

Please try this

strText =“This is Testing - 123 - 456”

strText.Split("-"c)(0).Trim

Working as expected. I have another question.
This is a test - 123 - 456

Assume that in place of hyphen we may get open bracket also. If that is the case i want to extract all those words before the open bracket. How to add that condition ?

strText =“This is Testing - 123 - 456”

strText.Split("("c)(0).Trim

while you are spiliting the string we just need to pass the delimeter from which to split the string like above i have use split("(")c)(0).trim to split semilarly you can just modify the delimeter according to the senario .

We are not aware that the line contains hyphen or bracket or what. is there is a way that whatever special characters comes bot will read all words before that ?

Hi @Balan

Please try this

System.Text.RegularExpressions.Regex.Matches(strText,"^[a-zA-Z\s]+")(0).ToString.Trim

1 Like

If you could provide some samples, we will try to build more generic pattern…

1 Like

It is something like we are doing a name matching. Input contains a name bot reads the name and going to mainframe screen which contains multiple row of records and bot needs to match the name in that. EX
Input is : Friday working
Mainframe contains multiple rows like
Siva
India
Friday working (weekend)
Sunday holiday - Leave
Bot has to extract words in each line and match with given input name.
In 3rd line bot need to extract words before open bracket and match the name.
If the input is Sunday holiday bot should read all words before hyphen and match the name.
This is the scenario i am working on.

Hi @Balan - Could you please check this…

https://regex101.com/r/ll2PKx/1/

Regex seems to be the answer, but you could also do something like:

if(strtext.Contains("-"), strText.Split("-“c)(0), strText.Split(”("c)(0))