RegEx Help!

I am trying to select a whole word under the cursor in a NSTextView

I found this piece of code, but it only returns from the Beginning of the word to the cursor

 var currentWord: String? {
        let regex     = try! NSRegularExpression(pattern: "\\S+$")
        let textRange = NSRange(location: 0, length: selectedRange.location)
        let text      = self.string
        if let range  = regex.firstMatch(in: text, range: textRange)?.range {
            return String(text[Range(range, in: text)!])
        }
        return nil
    }

So if I click on the “a” in “Equation” all I get back is “Equa”

the end of word will be a space or puncuation … . Numbers and () are considered part of the word

is the selectedRabge.Location perhaps the issue ?

this IS just a WAG

heck I dunno… I’m assuming that is the caret location…
but I tried to just “add” to that… and got super strange results

I don’t know what the pattern does… but if it just looks “back”, then I need something to also look “ahead”

well \S+ should match one or more non-whitespace characters
the $ is curious as that means, most times, the end of line

so that looks like “match non-whitespace characters up to the end of line”
and if the “line” ends before the entire word
ie if what its searching in is “Equa” then it makes sense it only find "“Equa”
there IS not more

maybe nslog the “text” ?

“text” is the entire contents of the NSTextView… So what it “looks” like it is doing
is searching from the beginning of the document to the cursor location (textRange)

and the line doesn’t end before the word does… I’ve tried dozens of points in the document, they all react the same …

Have I mentioned I hate RegEx? :slight_smile:

Figured out a way that did not require RegEx…

 var currentWord: String? {
        let textRange = NSRange(location: 0, length: selectedRange.location)
        self.moveWordBackwardAndModifySelection(self)
        self.selectWord(self)
        let text      = self.string
        return String(text[Range(selectedRange, in: text)!])
    }
1 Like

IMO any solution that eliminates regular expressions is usually the best one.

We got 15% performance boost in a text processing-heavy system just by putting some guard tests around Regex’s on hot paths, and leveraging short-circuit execution, e.g.:

if (name.StartsWith("FOO ", StringComparison.Ordinal) && Regex.IsMatch(name,"^FOO \d+$")) {
    // Do stuff here
}

The lift probably wouldn’t be as great today since MSFT has invested massively in improving the performance of their Regex engine and has added optimization hints we could have used a lot and improved how compiled Regex’s work. However … even assuming for the sake of argument that most of that 15% lift is now gone (a big assumption), there’s still the problem of intelligibility and maintainability.

No Joke: Ask ChatGPT for RegEx…

1 Like

I’ll ask ChatGPT

Please write me a complete IDE in Java for the Xojo Language
:stuck_out_tongue:

Once I stop getting “Sorry we’re at capacity right now” from ChatGPT
To paraphrase Adequately scaled ?