Solved

Regex: show the last n characters of a string

Posted on 2014-01-17
10
1,078 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
  • 4
  • 3
  • 2
10 Comments
 
LVL 34

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
 

Author Comment

by:Dallas Smetter
ID: 39794350
Wish I could give some points to Surrano.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 34

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
User profile Size Report 3 64
Python variable _ manually assigned 9 65
Advice in Xamarin 21 57
iSeries email authority 4 26
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

896 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now