Sumit Parakh
Sumit Parakh

Reputation: 1143

Regex to match last character of words having length greater than 1

I want to match last character of every word in a sentence in which , last character of only those words should be matched whose length is greater than 1.

For example, if sentence is:-

I love regex.

Then regex should match last character of love and regex only, i.e., e and x , not I.

So far i am able to match last character of every word, including those having length 1, using this regex :-

[a-zA-Z0-9](?= |\.|,|$)

But i want to match last character of only those words having length greater than 1. How can i do this?

Test link:- https://regex101.com/r/7tnXnB/1/

Upvotes: 2

Views: 2259

Answers (3)

Vasilis Nicolaou
Vasilis Nicolaou

Reputation: 71

I think this might work for you, although I'm sure there is a shorthanded version of this

[a-zA-Z0-9](?= [a-zA-Z0-9]|\.|,|$)

Edit:

\w(?= \w|\.|,|$)

Upvotes: 0

Aran-Fey
Aran-Fey

Reputation: 43166

You can use (negated) word boundaries \b and \B:

\B\w\b

Here \w matches a word character, \w\b asserts a word boundary (therefore it'll only match the last character in a word), and \B asserts that there is no word boundary before this character.

Upvotes: 8

Mr Mystery Guest
Mr Mystery Guest

Reputation: 1474

Try

[A-Z0-9a-z]{2,}

The {2,) makes sure it'll only get characters 2 or more of length

Upvotes: 0

Related Questions