?
Solved

Regex: show the last n characters of a string

Posted on 2014-01-17
10
Medium Priority
?
1,765 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 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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

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.

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Starting up a Project
Suggested Courses

777 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