Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Regex: show the last n characters of a string

Posted on 2014-01-17
10
Medium Priority
?
2,190 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
9 Comments
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Starting up a Project
Suggested Courses

810 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