Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing last character from a line using regex

Tags:

regex

I just started learning regex and I'm trying to understand how it possible to do the following:

If I have:

helmut_rankl:20Suzuki12
helmut1195:wasserfall1974
helmut1951:roller11

Get:

helmut_rankl:20Suzuki1
helmut1195:wasserfall197
helmut1951:roller1

I tried using .$ which actually match the last character of a string, but it doesn't match letters and numbers.

How do I get these results from the input?

like image 324
Audacity Shop Avatar asked Oct 22 '25 00:10

Audacity Shop


1 Answers

You could match the whole line, and assert a single char to the right if you want to match at least a single character.

.+(?=.)

Regex demo

If you also want to match empty strings:

.*(?=.)
like image 195
The fourth bird Avatar answered Oct 25 '25 05:10

The fourth bird



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!