Specific keyword matching in string using Regular expression

Hello guys,
I am facing an issue in matching a specific value in a string from a pool of keyword already provided using regex.
For example, i have to search for a keyword from a string stored in variable as str.
Keyword = “String Matching”
Str = “This is a String Matching from the string stored as variable”
I am using .
booloean = system.text.regularexpressions.regex.match(str, “^”+Keyword)
But it will only matches if keyword is at first location of string variable.

Please help me to get a regex which can also matches keyword present at the middle of string variable.

Thanks in Advance …!!

Instead of regular expressions, you can use simple string methods.

If yourString.Contains(substring) Then
// if string contains
Else
// not found
End If

If you still want to use regex, use the following,

booloean = system.text.regularexpressions.regex.match(str, “^\”+Keyword)

Regards,
Karthik Byggari

Hi Karthik,

Substring is having issue, For example:
Keyword= “string matching”
Str= “This is a string matching from the string stored as variable”
Str1=“This is a unstring matching from the string stored as variable”

Boolean= str.contains(Keyword)
and str1.contains(Keyword)

Both will return true is not the case i am looking for.

In this case, you need to just add spaces before and after.

yourString.Contains(" " + substring + " ")

It will ensure they are words.

Yes,

But if my string keyword is at the start then it won’t work
Ex :slight_smile:
str=“string matching stored as string”
then,
str.conatins.(" “+Keyword+” ")

It will not work in this case also because the string i am getting the keyword value is not fixed

I have edited my first post with regular expression. Try …

Yes saw edited part not working. what ("^"+Keyword) will do? anyhow it is not working too

Please try this -
Str.Contains(" " + Keyword + " ") or Str.StartsWith(Keyword)