I want to get a set of numbers (3 digits) from a string. But some numbers bind with specific text and those numbers don't need to be include in the output.
Input:
C123456 577 abcd 173944 C5678541883
Result should to be:
577 173 944 188
How can I achieve this?
I assume, you only want to match full numbers without any other characters, separated by white space. If you really want to capture and 188 too and 173944 split into two parts, you can provide a comment below with more information about what it actually should match (numbers with 3 or 6 characters, numbers etc.)
So, to extract all numbers from a white-space separated string, you can use the following regular expression. Make sure to use the global flag /g:
\b([0-9]+)\b
Here is a live example:
var text = "C123456 577 abcd 173944 C5678541883";
var regex = /\b([0-9]+)\b/g;
var match = regex.exec(text);
while(match !== null) {
console.log(match[1]);
match = regex.exec(text);
}
Related
I have a list of different items. Some of them have 8-10 digits in front of the name, some others have these 8-10 digits behind the name and some others again don't have these numbers in the name.
I have two expressions that I use to remove these digits, but I can not manage to combine them with | (or). They work each for themselves, but if I use the first expression first, then the second expression, I don't get the result I want to have.
I use these to expressions for now:
(?<=[\d]{8,10}) (.*)
.*?(?=[\d]{8,10})
But if I use them both (first one and then the other), then some of the lines become totally empty.
How can I combine these to to do what I want, or if it's better, write a new expression that does what I want to do :)
List is like this:
12345678 Book
12345678 Book
Book 12345678
Book 12345678
Cabinet 120x30x145
Want this result:
Book
Book
Book
Book
Cabinet 120x30x145
Why not just use the following.
Check if there are 8 numbers in the beginning of the string, or at the end of it and remove them.
(^\d{8,10}\s*|\s*\d{8,10}$)
It gives the wanted behaviour
Instead of only matching everything but a number containing
8-10 digits + adjacent spaces, use a regex to substitute
such a number (also + adjacent spaces) with an empty string.
To match, use the following regex:
*\d{8,10} *
That is:
* - a space and an asterix - a sequence of spaces (may be empty),
\d{8,10} - a sequence of 8 to 10 digits,
* - another sequence of spaces (may be empty).
The replacement string is (as I said) empty. Of course, you should use
g (global) option.
Note that you can not use \s instead of the space, as \s matches also
CR and LF and we don't want this.
For a working example see https://regex101.com/r/1hsGzT/1
You need to use \b meta sequence boundary:
/\b[0-9\s]{8,10}\b/g;
var str = `12345678 Book
12345678 Book
Book 12345678
Book 12345678
Cabinet 120x30x145`;
var rgx = /\b[0-9\s]{8,10}\b/g;
var res = str.replace(rgx, `\n`)
console.log(res);
I'm new on regular expression and I'm spending last two days about my problem.
I have a string like this:
38_285_4461_186_S2A_MSIL2A_20180119T101331_N0206_R022_T32TQQ_20180119T135441
and I need four regex expression to extraxt data from this string in four parts:
38
285
4461
186
I have more string to evaluate and this values are variable, each group contains only number, but number of digits are variable
example of string template are:
xx_xxx_xxxx_xx_S2...................... (where x is a digit and is variable)
I tried the following regex
^(?:[^_]*\_){1}([^_]*)
edit:
I need four regex expression, one for "group" and result is full match.
I can't use java. Regular expression will be used in geoserver.
http://docs.geoserver.org/latest/en/user/tutorials/imagemosaic_timeseries/imagemosaic_time-elevationseries.html
You could either use
^(\d+)_(\d+)_(\d+)_(\d+)
or simply split on the _ and use the array parts.
See a demo on regex101.com.
You don't need a regex to solve this problem, you can simply use split like this:
String[] values = "38_285_4461_186_S2A_MSIL2...".split("_")
// values[0] => 38
// values[1] => 285
// values[2] => 4461
// values[3] => 186
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.
I have a regex that already takes care of standardizing formatting of U.S. phone numbers, however it doesn't deal with a leading 1.
var cleanTelephoneNumber = function(tel) {
var regexObj = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
if (regexObj.test(tel)) {
return tel.replace(regexObj, "($1) $2-$3");
} else {
return null;
}
};
how can I get it to strip out a leading one if it exists and still continue to parse correctly
e.g.
+1-555-235-2444
1-555-235-2444
1.555.235-2444
1 555 235 2444
555-235-2444
should all translate to
(555) 235-2444
I'd like to just modify the regex I already have
/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/
You can modify your regex to use this:
^(?:\+?1?[-.\s]?)(\d{3})([-.\s])(\d{3})\2(\d{4})$
Working demo
The idea of the regex is:
^(?:\+?1?[-.\s]?) can have +1 and a separator
(\d{3}) must contain 3 digits
([-.\s]) store a separator
(\d{3}) follow by 3 digits
\2 use the same separator
(\d{4})$ follow by 4 digits
i want to validate my phone number with the regex for following formats.i have googled the things but i could not find the regex for following formats...
079-26408300 / 8200
(079) 26408300
079 264 083 00
9429527462
can anyone please guide me how can i do validate the phone number field for above formats?
I want to validate the phone number for only above formats as right now am using only following regex var phone_pattern = /^[a-z0-9]+$/i;
#Ali Shah Ahmed
var phone_pattern = "(\d{10})|(\d{3}-\d{8}\s/\s\d{4})|((\d{3}\s){3}\d{2})|((\d{3})\s\d{8})";
here is the way am checking if its valid
if (!phone_pattern.test(personal_phone))
{
$("#restErrorpersonalphone").html('Please enter valid phone number');
$("#personal_phone").addClass('borderColor');
flag = false;
} else {
$("#restErrorpersonalphone").html('');
$("#personal_phone").removeClass('borderColor');
}
its not working. Am I implementing in wrong way?
lets start with the simplest phone number 9429527462
As this has 10 characters and all are numbers, regex for it could be \d{10}
Now the next phone number 079 264 083 00. Regex for this pattern could be (\d{3}\s){3}\d{2}
First we are expecting a group of 3 digits and a space to repeat thrice (\d{3}\s){3}, this will cover 079 264 083 (space included in it), so left will be the last two characters which are handled using \d{2}
For the phone number (079) 26408300, \(\d{3}\)\s\d{8} regex could be use. The regex first looks for a opening bracket, then three digits inside it, and then the closing bracket. It then looks for a space, and then for 8 digits.
The phone number 079-26408300 / 8200 could be validated using regex \d{3}-\d{8}\s/\s\d{4}. It first looks for 3 digits then a -, then 8 digits followed by a space. Then looks for a / and then a space and then 4 digits.
If you wish to know a single regex for validating all the above patterns, do let me know.
Final combined regex would be:
/(\d{10})|(\d{3}-\d{8}\s\/\s\d{4})|((\d{3}\s){3}\d{2})|(\(\d{3}\)\s\d{8})/
Straightforward solution is simple, use |
String ex = "\\d{3}-\\d{8} / \\d{4}|\\(\\d{3}\\) \\d{8}|...