Regex to keep the last 4 characters of a string of unknown length


Regex to keep the last 4 characters of a string of unknown length



I need to use Regex to keep the last 4 characters of a string. I don't know the length of the string so I need to start at the end and count backwards. The program is written in c#.



Below are two examples:


840057


1002945



I need to be left with:


0057


2945



I have tried ^....(.*) and ^.{0,4} but this leaves me with 3 or 4 characters depending on the length of the string.


^....(.*)


^.{0,4}



When I try .{4}$ I am left with 100 and not the last 4 digits 2945. Below is a code snippet which may help.



code snippet



Thank you, I appreciate the help.





and if the string isnt 4 chars long?
– Gordon
Jun 29 at 12:42





this would work /....$/
– Harpreet Singh
Jun 29 at 12:43





The string is at least 6 characters, sorry I should have said that.
– user1226884
Jun 29 at 12:44




4 Answers
4



You don't need to use regex for that purpose.


string MyLast4Characters = MyString.Substring(((MyString.Length >= 4) ? (MyString.Length - 4) : (0)));



That part ((MyString.Length >= 4) ? (4) : (0)) is made to check if the original string is longer or equal to 4 characters, then it will return the lasts 4 characters, else the whole string


((MyString.Length >= 4) ? (4) : (0))





In this situation, I need to use regex, unfortunately. I appreciate the code snippet though.
– user1226884
Jun 29 at 12:45



.*(?=.{4})$



will match everything up to the four last characters of the string. If you replace that match with String.Empty, only those four characters remain.


String.Empty



If the string contains fewer than four characters, they will remain in the string because the regex won't match at all so there is nothing to replace.





Maybe I am missing something but when use .{4}$ I am left with 100 from 1002945 not the last 4 digits.
– user1226884
Jun 29 at 12:50





This shouldn't be the case. How do you call your regex capture?
– Cid
Jun 29 at 12:53





By the way, his answer is certainly the best
– Cid
Jun 29 at 12:56





Your question was unclear - we all interpreted it as "I want to match the last four characters of the string". You want to match everything but the last four characters and replace them with the empty string. Please be specific when writing a question :)
– Tim Pietzcker
Jun 30 at 7:12



If this has to be regex, I think you want:
.{4}(?=s|$)


.{4}(?=s|$)



But I agree that regex probably is not the best solution here.



A breakdown:



. : any character
{4} : exacty four times
(?= : followed by
s : white space
| : or
$ : a line ending
) : end the followed by section


. : any character
{4} : exacty four times
(?= : followed by
s : white space
| : or
$ : a line ending
) : end the followed by section





When use .{4}(?=s|$) I am left with 100 from 1002945, not the last 4 digits, there maybe something wrong in my implementation.
– user1226884
Jun 29 at 12:55



I guess this is something with your RegexOptions. In my example I use SingleLine mode ((?s)) and multi-line string:


RegexOptions


SingleLine


(?s)


static void RegexTest()
{
string str = "i am long stringrnwith the number 1002945";
string pattern = @"(?s)[0-9]{4}$"; // or @"(?s).{4}$"
string num = Regex.Match(str, pattern).Value;
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux