Question
· Apr 7, 2021

string search from list of keywords

anyone know of a simple way to search a blob of text from a list of keywords in one fell swoop, without having to iterate through each keyword and perform an individual search? Ex. 

s keywords="This,blob,text"

s text = "This is a sample blob of text"

i text[keywords w "hit"

Discussion (2)0
Log in or sign up to continue

See %Regex.Matcher

Example:

text "This is a sample blob of text",
  keywords="This,blob,text"

matcher=##class(%Regex.Matcher).%New($tr(keywords,",","|")),
  matcher.Text=text
w:matcher.Locate() "hit",!

matcher.ResetPosition()

while matcher.Locate() {write "Found ",matcher.Group," at position ",matcher.Start,!}

USER>^test
hit
Found This at position 1
Found blob at position 18
Found text at position 26

Or see $locate: Using Regular Expressions in Caché

Example:

USER>w $locate(text,$tr(keywords,",","|"),1,e,x)
1