I need to come up with "Regular expression" and a "Substitute" to pad any string that's shorter than 10 characters with zeros. It has to work on regex101.com, PHP flavor. This is all I need.
Example Input:
123
12345
1234567891
Expected output:
0000000123
0000012345
1234567891
I wish it was simple as searching for ([0-9]{1,9}) and replacing it with 000000000$1 but obviously string would exceed length of 10 characters. So I am trying with read ahead syntax but no luck.
As you mentioned in the comments below your question, I provided a .NET method using a catalog to pad a string in regex without using a conditional replacement (see my answer here).
This answer can be adapted to PCRE by using a branch reset group (?|...).
See regex in use here
Options gJsm and substitution of ${x}$1
^((?|[1-9](?=.*1\t+(?<x>0+))|[1-9]\d(?=.*2\t+(?<x>0+))|[1-9]\d{2}(?=.*3\t+(?<x>0+))|[1-9]\d{3}(?=.*4\t+(?<x>0+))|[1-9]\d{4}(?=.*5\t+(?<x>0+))|[1-9]\d{5}(?=.*6\t+(?<x>0+))|[1-9]\d{6}(?=.*7\t+(?<x>0+))|[1-9]\d{7}(?=.*8\t+(?<x>0+))))\b
The result:
1
12
123
12345678
123456789
Becomes...
000000001
000000012
000000123
012345678
123456789
Related
I have this expression in PCRE and I want to leave/remove the . (period) out of klantnummer.
Expression:
^h:\/Klant\/(?<klantnummer>[^\/]+)\/(?<folder1>[^\/]+)\/(?<folder2>[^\/]+)
Input:
h:/Klant/12345678.9/map 1/map 2
Outcome: 12345678.9
Desired result: 123456789
https://regex101.com/r/EVv47V/1
So Klantnummer should have 123456789 as result
You can't do that in one step. You could catch it in two Capture Groups:
^h:\/Klant\/(?<klantnummer1>[^\.\/]+)\.(?<klantnummer2>[^\/]+)\/(?<folder1>[^\/]+)\/(?<folder2>[^\/]+)
and put both together by string concatenation after or use two regex steps and filter out the period in the second, like stated in comments.
Regex above assumes there is always a period, this will work for 0 or 1 period in the number:
h:\/Klant\/(?<klantnummer1>[^\.\/]+)(?:\.?(?<klantnummer2>[^\.\/]+))\/(?<folder1>[^\/]+)\/(?<folder2>[^\/]+)
As already discussed you can't do this on one step.
The solution of using 2 regex stages, or 2 splitting klantnummer into 2 groups before and after the capture group will both work.
However I believe that the simplest and most efficient both in terms of computer power and of code to write, will be to replace .with and empty String '' after the regex, and before using it.
You haven't said which programming language you are using so I can't give you the syntax/example.
If all that you are doing is splitting the String on the slashes you will probably find it easier to split the string into an array.
For example in python
s = "h:/Klant/12345678.9/map 1/map 2"
array = s.split('/')
Klantnummer=array[2].replace('.','')
folder1=array[3]
folder2=array[4]
print(Klantnummer)
print(folder1)
print(folder2)
output
123456789
map 1
map 2
Tested on https://www.online-python.com/
I'm a regex newbie and I've got a valid regex for SSNs:
/^(\d{3}(\s|-)?\d{2}(\s|-)?\d{4})|[\d{9}]*$/
But I now need to expand it to accept either an SSN or another alphanumeric ID of 7 characters, like this:
/^[a-zA-Z0-9]{7}$/
I thought it'd be as simple as grouping the SSN and adding an OR | but my tests are still failing. This is what I've got now:
/^((\d{3}(\s|-)?\d{2}(\s|-)?\d{4})|[\d{9}])|[a-zA-Z0-9]{7}$/
What am I doing wrong? And is there a more elegant way to say either SSN or my other ID?
Thanks for any helpful tips.
Valid SSNs:
123-45-6789
123456789
123 45 6789
Valid ID: aCe8999
I have modified your first regex also a bit, below is demo program. This is as per my understanding of the problem. Let me know if any modification is needed.
my #ids = (
'123-45-6789',
'123456789',
'123 45 6789',
'1234567893434', # invalid
'123456789wwsd', # invalid
'aCe8999',
'aCe8999asa' # invalid
);
for (#ids) {
say "match = $&" if $_ =~ /^ (?:\d{3} ([ \-])? \d{2} \1? \d{4})$ | ^[a-zA-Z0-9]{7}$/x ;
}
Output:
match = 123-45-6789
match = 123456789
match = 123 45 6789
match = aCe8999
Your first regex got some problems. The important thing about it is that it accepts {{{{}}}}} which means you have built a wrong character class. Also it matches 123-45 6789 (notice the mixture of space and dash).
To mean OR in regular expressions you need to use pipe | and remember that each symbol belongs to the side that it resides. So for example ^1|2$ checks for strings beginning with 1 or ending with 2 not only two individual input strings 1 and 2.
To apply the exact match you need to do ^1$|^2$ or ^(1|2)$.
With the second regex ^[a-zA-Z0-9]{7}$ you are not saying alphanumeric ID of 7 characters but you are saying numeric, alphabetic or alphanumeric. So it matches 1234567 too. If this is not a problem, the following regex is the solution by eliminating the said issues:
^\d{3}([ -]?)\d\d\1\d{4}$|^[a-zA-Z0-9]{7}$
I need a regular expression that would find 100 ABCDEF from input string Suite 400 - 100 ABCDEF. It should be noted that I created a regex as below but it picks the value from Suite.
[^-\s]\d.+
Just put $ at the end of your regex. $ means "end of line".
Also, replace the dot with [^-], so it will match only non-hyphens:
[^-\s]?\d[^-]+$
Fiddle: http://refiddle.com/refiddles/5b9a88ef75622d4ca9590000
Since you're trying to match a US street address, you should try matching a number followed by one or more words instead:
\d+(?:\s+[A-Za-z.]+)+
Demo: https://regex101.com/r/y6n5jD/1
I want to allow only some specific types of phone number format.
Ex:
xxx-xxx-xxxx
+91-xxxxxxxxxx.
I don't know what will be the regular expression for this.
I refered some sites and got this
/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/
which works for 1st one but not worked for +91 format.
Basically, I want to allow India and US numbers only.
2nd Question:
I want a regular expression which will allow +, -, (, ), and the numbers i.e. 0-9 .
Here is a suggestion that accept your inputs, with separators , . and -:
\+91[\s-]\d{10}|\(?\d{3}\)?[\s-]\d{3}[\s-]\d{4}
Try it on regex101
The following inputs are valid:
123-456-7890
123 456 7890
(123) 123-6547
(999)-999-9999
+91-1234567890
+91 1234567890
If you only want to accept - as a separator, change all [\s-] by - in the regex.
are u looking for something like ^(\d{3}-\d{3}-\d{4}|\+91 \d{10})
example
both numbers are placed in group one this way if u work with groups
edit:
are u looking for one like this? this allows the sets of numbers to be seperated with everything exept a letter or a number (\d{3}[^\d\w]\d{3}[^\d\w]\d{4}|\+91[^\d\w]\d{10})
example
I am working on siebel CRM. I have space issues in my regex.
I have SSN numbers in these formats
123 456 789
123-456-789
123 45 6789
I need to dispaly my SSN Like XXX-XX-4567. My regex looks like
([\s.:])(?!000)(?!666)(?!9[0-9][0-9])\d{3}[- ]?(?!00)\d{2}[- ]?(?!0000)\d{4})([\s.:]) |
([\s.:])(?!000)(?!666)(?!9[0-9][0-9])\d{3}[- ]?(?!00)\d{3}[- ]?(?!00)\d{3})([\s.:]).
How can I remove all blank spaces in the above expression and display the format as i mentioned above?
It looks like there are syntax errors in your RegEx. There are a couple of unmatched brackets, at (?!0000)\d{4}) on the first section, the last bracket is unmatched.
I think I've managed to write the regex you're looking for, but a bit shorter than the one you were using:
([\s.:])((?!000)(?!666)(?!9[0-9]{2})\d{3})[- ]?((?!00)\d{2,3})[- ]?((?!00)\d{3,4})([\s.:])
This will match the following strings:
123-12-1234
123 456 789
123-456-789
123 45 6789
But will not match the following:
666-45-1234
abc-12-1232
123-00-1233
123-224-0011
123 224 0000
There are several capture groups here:
Matches any character (you may want to change this).
Matches the first three digit number.
Matches the second, two or three digit number.
Matches the third, three or four digit number.
Matches any character (you may want to change this).
You should be able to reconstruct the SSN in the format you need with the result of this RegEx.