Skip to main content
Inspiring
December 15, 2023
Answered

How to grep the last letter in a consecutive second word?

  • December 15, 2023
  • 3 replies
  • 773 views

It’s possible to find the last letter?


S E A R C H:
my ?????e

my= is a the first word in the search (may be ours, yours, any word)

?????e = is the second word where grep must detect the last letter, e.


R E S U L T S:

my eye

my fate
my shame

my aerogramme

Thanks
ps. Have a very precarious grep that only catches the condition in a word: (o)h?\b

This topic has been closed for replies.
Correct answer m1b

Hi @palala fog, so you want a separate grep that will select two words my * ?

(\bmy)\s([[:alpha:]]+\b)

3 replies

Inspiring
December 16, 2023

hi m1b,

grep is working as it recognizes words ending in a specific letter.

Is it possible (since I didn't mention it) that the result shows the two words involved?
Tried to do it but the expression is a unity.

the idea is to catch both to make lists.
O U T P U T
my eye
my rate

thanks.

Mayhem SWE:
thanks. your grep is almost fine but doing this:



Mayhem SWE
Inspiring
December 16, 2023

I don't understand what problem your are trying to solve, nor what the issue with my regex was.

 

Exactly which characters did you expect to be highlighted in your screenshot?

Is the problem that it includes punctuation (the comma)? Try something like this:

\w\s+\K\w*\K\w

Should it only have highlighted the  "t" at the end of "aut" and nothing else? Try:

\w\s+\K\w*\K\w(?=.*)

 

FRIdNGE
December 16, 2023

if just paras with always 2 words:

 

.$

 

If paras with any length of words (1, 2, 3 or more):

 

(?:^\S+\s\S*)\K\S

 

(^/)  The Jedi

m1b
Community Expert
Community Expert
December 15, 2023

Hi @palala fog, here's my attempt:

\bmy\s\b[^\h\n\r]+\K[[:alpha:]]\b

- Mark

Mayhem SWE
Inspiring
December 15, 2023

Interesting! Regex engines normally won't do variable length lookbehind., but I did not know about \K before.

 

Not knowing anything more about the OP's requirements this seems simple and work good enough:

 

\S +\K\S*\K\S
Mayhem SWE
Inspiring
December 15, 2023

Something was broken with my copy paste, should've been a \s+ where a space snuck in and posts cannot be edited?!

\S\s+\K\S*\K\S