Solved

Regex: show the last n characters of a string

Posted on 2014-01-17
10
1,546 Views
Last Modified: 2014-01-22
I have 36 characters strings, and I need to find out how to use a regex to just show the last 4 characters, whatever they may be.

Thank you in advance!
0
Comment
Question by:Dallas Smetter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
10 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39789768
Assuming each 36 char string is on it's own line, you can use this:
^[^\n]+?([^\n]{4})$

and you'll have the last 4 characters in group 1.

In English: match the beginning of the line (^) and then as many non end-line characters as you can before the last 4 non end-line characters.

PS: why don't you use right(string, 4) in your favorite language?

HTH,
Dan
0
 

Author Comment

by:Dallas Smetter
ID: 39789787
Dan, yes I would just write the normal code but... in this particular instance, it's a front end management GUI that only accepts a regex as a parameter. I don't have access to the backend on this baby. Thanks!
0
 
LVL 8

Expert Comment

by:Surrano
ID: 39793898
Another solution:

(....)$

simple as it is.
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:Dallas Smetter
ID: 39794350
Wish I could give some points to Surrano.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39794369
You can.
Click on "Request attention" and ask a mod to reopen the question.
Then you can redistribute the points as you see fit.

HTH,
Dan
0
 

Author Comment

by:Dallas Smetter
ID: 39794376
Step one has been performed.
0
 
LVL 8

Expert Comment

by:Surrano
ID: 39794900
C'mon I already saw that the question was closed before I answered. No need to redistribute points. Really.
0
 

Author Comment

by:Dallas Smetter
ID: 39794905
Suit yourself, I was just trying to do what I thought was right. Damned if you do, damned if you don't...
0
 
LVL 8

Expert Comment

by:Surrano
ID: 39795287
Not damned... You just embarrass me :-)
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question