2

I am trying to search backwards (using ?) for the beginning of a "word".

I don't mean a word in the text object sense; I mean a series of lower-case letters optionally beginning with a capital letter. The optional capital letter is stymying me.

Here are some examples showing where I want to jump to, assuming the cursor starts at the end of the line:

quick brown fox
^     ^     ^

QuickBrownFox
^    ^    ^

Quick Brown Fox
^     ^     ^

quick_brown_fox
^     ^     ^

The closest I've come up with is ?\(\U\u\)\|\L?e+ – but that doesn't work with the capital letters. I assume this is because \L matches before \U\u does.

How can I solve this?

3

This simple search works for all the examples you gave:

?\a\l\+

It matches any alphabetic character (upper or lower case), followed by any non-zero number of lower case characters.

Some examples of cases you didn't mention where I'm a little unclear what you want it to do:

match a lower case
MatchACapital

If you want it to match the single letter word a/A in both of these, then use a * instead of the \+:

?\a\l*
| improve this answer | |
  • @andy-stewart I'm also unclear on whether you need the regexp to only match the first letter in the word. My answer moves the cursor in the way you describe, but matches the whole word. – Rich May 26 '17 at 11:06
  • Much iimprove, so amaze, wow – user1685095 May 26 '17 at 12:20
  • This however wouldn't work with qui1ck brown fox because of the number, but can bi fixed with something like [a-zA-Z][a-zA-Z0-9]\+ – user1685095 May 26 '17 at 13:42
  • @user1685095 Yes! But again, it's not clear from the question what the OP actually would want it to do in that situation. – Rich May 26 '17 at 13:45
  • yes, absolutely. It's probably about programming. So digits inside a word are possible. But still that's a conjecture. Judging by his silence - he's already solved the problem by himself. – user1685095 May 26 '17 at 13:59
0

Although ugly this does what you need. At least in your example. I think it could be simplified, but wouldn't bother to do so. \(\<\|\([a-z0-9]\?\(\( \|_\)\zs\|\zs\u\)\)\)

With very magic \v(<|([a-z0-9]?(( |_)\zs|\zs\u)))

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.