How to create regex that adds a space after every 3-4 letters? - regex

how to create a regex that automatically adds a space after every 3-4 letters?
I currently have a regex created, but it will add a space only after adding a letter to the input:
function format(s) {
return s.toString().replace(/\d{3,4}?(?=..)/g, "$& ");
}
console.log(format(1234567890));
console.log(format(123456789));
console.log(format(1234567));
console.log(format(123456));
console.log(format(1234));
console.log(format(123));
So the output is like this.
123 456 7890
123 456 789
123 4567
123 456
1234
123
How to rebuild this regex so that the output is like this?
123 456 789 0
123 456 789
123 456 7
123 456
123 4
123
Thank you.

Currently, .. is requiring two characters after the match. If you want to allow one character after the match, remove one of the wildcards:
function format(s) {
return s.toString().replace(/\d{3,4}?(?=.)/g, "$& ");
}
console.log(format(1234567890));
console.log(format(123456789));
console.log(format(1234567));
console.log(format(123456));
console.log(format(1234));
console.log(format(123));
This produces the exact output you are looking for.

If you have only digits, you can also match 3 digits and assert a non word boundary after it using the pattern \d{3}\B
function format(s) {
return s.toString().replace(/\d{3}\B/g, "$& ");
}
console.log(format(1234567890));
console.log(format(123456789));
console.log(format(1234567));
console.log(format(123456));
console.log(format(1234));
console.log(format(123));

Related

python multiline regex capture

I have the following string:
hello
abcd
pqrs
123
123
123
My objective is to capture everything starting hello and till the first occurrence of 123.
So the expected output is as:
hello
abcd
pqrs
123
I used the following:
output=re.findall('hello.*123?',input_string,re.DOTALL)
But the output is as:
['hello\nabcd\npqrs\n123\n123\n123']
Is there a way to make this lookup non-greedy using ? for 123? Or is there any other way to achieve the expected output?
Try using lookhead for this. You are looking for a group of characters followed by \n123\n:
import re
input_string = """hello
abcd
pqrs
123
123
123"""
output_string = re.search('[\w\n]+(?=\n123\n)', input_string).group(0)
print(output_string)
#hello
#abcd
#pqrs
#123
I hope this proves useful.

Basic US phone regex not picking up area code

I made this regex (\([0-9]\)\s)?([0-9]{3})([\s-])([0-9]{4}) to find 4 basic phone formats.
555-1234
555 1234
(414) 555-1234
(414) 555 1234
It's not finding the phone numbers with the area code in parenthesis
Your requirements are senselessly rigid. These are all valid US phone numbers
1234567
123 4567
123-4567
123 - 4567
123 456 7890
123.456.7890
(123) 4567890
+1 (123) 456-7890
+1 123-456-7890
And countless other permutations.
Don't be a pain in your user's neck. Write regexps that are friendly
/^(?:\+1\s*)?(?:(?:\(\d{3}\)|\d{3})\s*[.-]?\s*)?\d{3}\s*[.-]?\s*\d{4}$/
This allows for:
optional leading region code +1 (North America)
optional spaces, ., -, or combination of these to be used as a separator
optional area code
optional area code wrapped in ()
If you want to accept a wide variety of user inputs but store them in a uniform format in your system, that's easy too. Use capture groups in the regexp to extract the numbers that are relevant.
/^(?:\+1\s*)?(?:(?:\((\d{3})\)|(\d{3}))\s*[.-]?\s*)?(\d{3})\s*[.-]?\s*(\d{4})$/
To create capture groups, you can use (). Notice the use of capture groups around \d{3} and \d{4}.
Now in our code, we can use capture groups. This example code uses JavaScript
const truthy = x => !!x;
const formatPhoneNumber = n => {
// regexp that we made above
let re = /^(?:\+1\s*)?(?:(?:\((\d{3})\)|(\d{3}))\s*[.-]?\s*)?(\d{3})\s*[.-]?\s*(\d{4})$/;
// destructure the capture groups
let [match, area1, area2, part1, part2] = re.exec(n);
// reassemble the parts as desired
return [area1 || area2, part1, part2].filter(truthy).join('-')
}
Now let's define our sample data and see how each number gets formatted
let xs = [
'1234567',
'123 4567',
'123-4567',
'123 - 4567',
'123 456 7890',
'123.456.7890',
'(123) 4567890',
'+1 (123) 456-7890',
'+1 123-456-7890',
];
xs.map(formatPhoneNumber);
Notice each phone number input was vastly different, but the output is 100% uniform.
[
"123-4567",
"123-4567",
"123-4567",
"123-4567",
"123-456-7890",
"123-456-7890",
"123-456-7890",
"123-456-7890",
"123-456-7890"
]
This worked:
/(\([\s\d]+\)\s*)?\d+[\s\-]*\d+/g

SSN issue in SIEBEL

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.

Regex to validate 13 digit telephone number is numeric and can contain maximum of 3 spaces

I would like to validate a telephone number which can contain 10 to 13 digit numbers and can contain 0 to 3 spaces (can come anywhere in the data). Please let me know how to do it?
I tried using regex ^(\d*\s){0,3}\d*$ which works fine but I need to restrict the total number of characters to 13.
You want to match same text against 2 different whole-line patterns.
It's achievable either by matching patterns consequently:
$ cat file
1234567 90
1234567890
123 456 789 0123
123 456 789 01 23
$ sed -rn '/^([0-9] ?){9,12}[0-9]$/{/^([0-9]+ ){0,3}[0-9]+$/p}' file
1234567890
123 456 789 0123
$
Or if Your regex engine (perl/"grep -P"/java/etc) supports lookaheads - patterns can be combined:
// This is Java
Pattern p = Pattern.compile("(?=^([0-9] ?){9,12}[0-9]$)(?=^([0-9]+ ){0,3}[0-9]+$)^.*$");
System.out.println(p.matcher("1234567 90").matches()); // false
System.out.println(p.matcher("123 456 789 0123").matches()); // true
System.out.println(p.matcher("123 456 789 01 23").matches()); // false

POBox address capture

/^\s*((p(ost)?.?\s*(o(ff(ice)?)?)?.?\s+(b(in|ox))?)|b(in|ox))/i
Is the above the best regex to capture Post Office Box addresses?
Using the above, it failed on the ones marked below:
P.O. Box 123
PO 123
Post Office Box 123
P.O 123
Box 123
#123 // This one
123 // This one
POB 123
P.O.B 123 // This one
P.O.B. 123 // This one
Post 123
Post Box 123
Any more you can think of?
RegexLib.com has a pretty good list of a number of different expressions you can use.