How to return the first five digits using regular expressions

How to return the first 5 digits of a string of characters in regular expressions?

For example, if I have the following text:

15203 Main Street Apartment 3 63110

How can I return only "15203".

I am using C #.

+4
source share
7 answers

it will depend on your flavor of the Regex language and the coding language (C #, PERL, etc.), but in C # you would do something like

string rX = @"\D+"; Regex.replace(input, rX, ""); return input.SubString(0, 5); 

Note. I'm not sure about this regular match (others might be better here), but basically, since Regex doesn’t "replace" anything, just matching patterns, you have to look for any-digit characters; once you agree to this, you will need to replace it with the language version on the empty version (string.Empty or "" in C #), and then capture the first 5 characters of the resulting string.

+4
source

Actually, this is not a problem that is ideally solved using the single regex approach - the regular expression language is simply not intended for it. Assuming you are writing code in real language (and not some kind of poorly thought out built-in use of a regular expression), you could do (examples in perl)

 # Capture all the digits into an array my @digits = $str =~ /(\d)/g; # Then take the first five and put them back into a string my $first_five_digits = join "", @digits[0..4]; 

or

 # Copy the string, removing all non-digits (my $digits = $str) =~ tr/0-9//cd; # And cut off all but the first five $first_five_digits = substr $digits, 0, 5; 

If for some reason you are really stuck in a single match, and you have access to capture buffers and a way to combine them together, then the wdebeaum suggestion works very well, but it's hard for me to imagine a situation where you can do all this, but not have access to other language tools :)

+6
source

You can capture each digit separately and then add them together, for example. in Perl:

 $str =~ /(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)/; $digits = $1 . $2 . $3 . $4 . $5; 
+2
source

I don't think regex is the best tool for what you want.

Regular expressions match patterns ... the pattern you are looking for is "number (ny)"

Your logic, external to the pattern, is "five matches."

Thus, you either want to focus on the first five-digit matches, or capture five digits and combine them together .

But look at this Perl example β€” it's not one pattern β€” it repeats one pattern five times.

Can you do this through regex? Just like parsing XML - you probably could, but it's not the right tool.

+2
source

Not sure if this is best permitted by regular expressions, as they are used to match strings and, as a rule, not for string manipulations (in my experience).

However, you can call: strInput = Regex.Replace (strInput, "\ D +", ""); to delete all non-character characters, and then just return the first 5 characters.

If you just need a regex expression that does all this for you, I'm not sure if it exists without using the regex class like above.

+1
source

Another approach is

 #copy over $temp = $str; #Remove non-numbers $temp =~ s/\D//; #Get the first 5 numbers, exactly. $temp =~ /\d{5}/; #Grab the match- ASSUMES that there will be a match. $first_digits = $1 
+1
source

result = ~ s / ^ (\ d {5}). * / $ 1 /

Replace any text starting with the number 0-9 (\ d) exactly 5 of them {5} with any number of anything after it. * 's $ 1, which is what is contained in (), that these are the first five digits.

if you need the first 5 characters.

result = ~ s / ^ (. {5}). * / $ 1 /

Use any programming language that you use for evaluation.

t

 regex.replace(text, "^(.{5}).*", "$1"); 
-1
source

All Articles