Solved

Regex: show the last n characters of a string

Posted on 2014-01-17
10
998 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

743 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

14 Experts available now in Live!

Get 1:1 Help Now