Home > Mobile >  Extract first six digits with or without dots in between
Extract first six digits with or without dots in between

Time:06-16

Users may input strings such as the following:

4409101800
16.10.10.110
4409101800 - Lorem Ipsum is simply dummy text of the printing and typesetting industry.
16.10.10.110 - Lorem Ipsum is simply dummy text of the printing and typesetting industry.

A digital code is always in the beginning of the string.

I need only a string containing the first 6 digits including dots (if any).

Like this:

440910
16.10.10

I've tried to create a regex, but failed.

Could you help me find an elegant solution to this task?

CodePudding user response:

Capture the first 5 digits and add optionally many dots in between, and then match a final digit:

(\d\.*){5}\d

Regex101

CodePudding user response:

If you are okay with doing it pretty primitively, a simple loop and checking each character if they are a number or not, you could do something like below:

const input = [
  `4409101800`,
  `16.10.10.110`,
  `4409101800 - Lorem Ipsum is simply dummy text of the printing and typesetting industry.`,
  `16.10.10.110 - Lorem Ipsum is simply dummy text of the printing`
]

const getFirstSixNumbers = (str) => {
  const strArr = str.split('');
  let digitCount = 0;

  let i = 0;
  let output = '';
  while (digitCount < 6) {
    const nextChar = strArr[i];
    if (is_numeric(nextChar)) digitCount  ;
    output  = nextChar;
    i  ;
  }

  return output;
}

input.forEach(item => console.log(getFirstSixNumbers(item)));


//check if number
//source: https://stackoverflow.com/questions/8935632/check-if-character-is-number
function is_numeric(str) {
  return /^\d $/.test(str);
}

CodePudding user response:

You can run a single for loop for checking each character and the return then slice() of the string with the first 6 digits in it.

const arr=["4409101800",
"16.10.10.110",
"4409101800 - Lorem Ipsum is simply dummy text of the printing and typesetting industry.",
"Too 1 few 2.34 digits 5 available",
"And finally: 16.10.10.110 - Lorem Ipsum is simply dummy text of the printing and typesetting industry."];


function first6(str){
  for (var rx=/\d/, n=i=0;i<6&&n<str.length;n  ) 
    if(rx.test(str[n])) i  ;
  return str.slice(0,n)
}

arr.forEach(s=>console.log(first6(s)));


// And here is another way, solely using a regular expression:
const rx2=/\D*?(\d\D*?){6}/;
arr.forEach(s=>console.log((s.match(rx2)??[""])[0]));

The second, regular expression based, solution acts more strictly as it will return an empty string if the required 6 digits cannot be found in the input string.

CodePudding user response:

You can use the following regular expression:

/((\d\.?){6})/m

It will match any digit, and eventually a dot that may or may not be present (the ? makes it optional). It will only match if the pattern is repeated 6 times, and the result is wrapped in a group. The m flag makes it working also on several lines

const regex = /((\d\.?){6})/m

const str1 = '4409101800'
const str2 = '16.10.10.110'
const str3 = '4409101800 - Lorem Ipsum is simply dummy text of the printing and typesetting industry.'
const str4 = '16.10.10.110 - Lorem Ipsum is simply dummy text of the printing and typesetting industry.'
const str5 = '01 01'
const str6 = 'No numbers'

console.log(str1.match(regex)?.[1]);
console.log(str2.match(regex)?.[1]);
console.log(str3.match(regex)?.[1]);
console.log(str4.match(regex)?.[1]);
console.log(str5.match(regex)?.[1]);
console.log(str6.match(regex)?.[1]);

  • Related