Sublime Forum

Capturing a literal string within a literal string

#1

Hello. My name is Stupid.

Per good developer practices I’ve read the Fine manual. But either I have missed something or fundamentally I just “don’t get it” —yet. I’m new to this sublime ship, so please, forgive the Stupid. Now down to brass tacks:

Say out of a paragraph of many sentences, I wish to target a specific sentence for highlighting: “The quick brown fox jumps over the lazy dog.”

I can do this from within a myStupid.sublime-syntax file:

  • match: “\bThe quick brown fox jumps over the lazy dog.\b”
    scope: text.plain.stupid.sentenceOne

I can do this from within a MyStupidSyntax.tmLanguage file and get the same result:

[PackageDev] target_format: plist, ext: tmLanguage
name: Stupid_Syntax
scopeName: text.plain.stupid
fileTypes: [txt]

patterns:

  • name: test.plain.stupid.sentenceOne
    match: ‘\bThe quick brown fox jumps over the lazy dog.\b’

QUESTION: How does one capture a string literal within a string literal?

  1. If my “regular expression” is a whole sentence —a specific sentence.
  2. A word within that sentence is important: “brown” —because we all know foxes are really red.
  3. How do I capture “brown” without an absolutely horrid regular expression?

I can do this to dissect the sentence, but this opens Pandora’s box, with respect to regular expressions.
This is essentially a word based decomposition of the sentence.
patterns:

  • name: test.stupid.sentenceOne
    match: ‘(\bThe\b) (\bquick\b) (\bbrown\b) (\bfox\b) (\bjumps\b) (\bover\b) (\bthe\b) (\blazy\b) (\bfox\b)’
    captures:
    ‘1’: {name: wordOne}
    ‘2’: {name: wordTwo}
    ‘3’: {name: wordThree}
    ETC…

The other option might be to do three capture groups:
begin (The)
middle (quick brown fox jumps over the lazy)
end (dog)

But god almighty. I just want to know whether a specific literal word or sequence of words (phrase), exists within (is a subset of) a specific literal sentence. That is to say,
“brown fox”, exists within “The quick brown fox jumps…”

It seems, that I don’t quite understand nested scopes or scope encapsulation yet with Sublime. I have tried many other things, as is usual to the introvert that does not come to the boards lightly. But I am out of time.

Thank you anyone, anywhere, for any help…

Stupid. :slight_smile:

1 Like

#2

To me, it sounds like your actual problem lies within the understanding of how regex captures work in this context. You seem to have a general idea but are not sure how to apply it in order to solve your problem.

Assuming you know about regex and your example is canon, you could simply wrap the text you would want to highlight in a special matter in a capture group and apply a context to it. In .sublime-syntax syntax:

- match: "\bThe quick (brown fox) jumps over the lazy dog.\b"
  scope: text.plain.stupid.sentenceOne
  captures:
    1: word.brown-fox

If you want this to be more “dynamic”, i.e. not have everything determined in a single expression, you’ll need to be more creative, but this is not clear for your example. I presume that help would be more accurate if you posted your actual example.

0 Likes

#3

Thanks FichteFoll, that got me where I needed to go. :slight_smile: Regex == my personal f’in kryptonite.

0 Likes

#4

 

RegExr

RegExOne

RegEx101

1 Like

#5

I can personally recommend http://www.regexbuddy.com/ on Windows, though it isn’t free, it is well worth it IMO :slight_smile:

2 Likes