Nested patterns to find the text

How I can use a pattern to find a text like below:

---- My actual text will be one of these things: 
---   "Hello one" or "Hi Person Great" or "One"
set my_ptn to <"Hello One" or (maybe 1 or 2 words, "Person Great") or (Not preceded by 2 chars, "One")>

So, I used the pattern like above in the ad-hoc box and it’s working, but when I bring it to the code, it is not accepting as a valid syntax. So, how can I achieve to match this pattern? Any help is appreciated. Thanks.

There are a couple of problems here. The first is that “words” is not defined in the pattern language in the way you’ve used it. You can talk about “word characters” or “word breaks” but not “words”. So it’s assuming that “words” in your pattern is a variable, which hasn’t been given a value yet in your script so it’s just the literal string “words”.
The second problem is that “maybe 1 or 2 words” is interpreted as: maybe the character “1”, or else 2 words (whatever they are).
I would suggest a pattern like:

set fixed_ptn to <"Hello One" or (maybe characters, "Person Great") or (start of text, "One")>

If it’s important that “Person Great” is preceded specifically by one or two words and not just an arbitrary number of characters, you could do something like this:

set words to <start of word, chars, end of word, maybe whitespace>
set my_ptn to <"Hello One" or (maybe (1 to 2 words), "Person Great") or (Not preceded by 2 chars, "One")>

Here I’ve defined words to be a pattern that describes a word along with following whitespace. Then I’ve used that in the sub-pattern 1 to 2 words (using “to”, not “or”).

3 Likes

I did not know about <start of text> until now. Where is it mentioned in the documentation? Thank you for pointing at it.

Thanks @SenseTalkDoug . This helps.

Here you can find the the start text: Anchors with SenseTalk's Pattern Language | EPF Docs

These suites about pattern might be interesting for you as well

note that each of those has a video on youtube. Links are in the readme. Allowing you to listen and watch Doug in action :slight_smile: here is the full list of videos.
Cheers,
Karsten

1 Like