How to recover the number of characters via regular expression?

Asked

Viewed 1,803 times

7

How to know the number of repeated characters (example: Goitobto = 2 letters to), consecutive characters (sequence of letters abc..) and consecutive numbers (example: 12345..) within a word, using regular expression? This is possible?

  • 2

    Example in javascript: 'teste'.match(/e/g).length returns 2 as it has two letters E

  • Correct @Wallacemaxters is exactly this.

  • I already have something like this: Click Here!, but it’s not working properly.

  • It is PHP or javascript ?

  • The function I’m doing is in Javascript.

  • You won’t be able to do that without restricting it to a specific letter, like Wallace did.

Show 1 more comment

2 answers

2


With regular expressions it should not be possible to do this as quoted in reply of Miguel Angelo, however, through a loop you can get the amount of occurrences of the characters and store them in a associative array.

var str = "Goiaba";
var ocorrencias = {};

for(var i = 0; i < str.length; i++) {
    var n = str[i];
    ocorrencias[n] = ocorrencias[n] ? ocorrencias[n] + 1 : 1;
}    
console.log(ocorrencias); // {G=1, o=1, i=1, a=2, b=1}

DEMO

To make it more beautiful and organized do it in a function:

function retornarOcorrencias(str){
    var ocorrencias = {};
    for(var i = 0; i < str.length; i++){
        var n = str[i];
        ocorrencias[n] = ocorrencias[n] ? ocorrencias[n] + 1: 1;
    }
    return ocorrencias;
}

var str = 'Goiaba';
var num = '001549954607410';

var strItens = retornarOcorrencias(str); 
var numItens = retornarOcorrencias(num); 

console.log(strItens); // {G=1, o=1, i=1, a=2, b=1}
console.log(numItens); // {0=4, 1=2, 4=3, 5=2, 6=1, 7=1, 9=2}

DEMO

To check the number of occurrences of a particular character:

console.log(strItens['a']); // 2

In the answers below shows how to do this in PHP:

  • I made some modifications here and the function worked perfectly, thank you for curing my doubts vlw!.

1

I guess there’s no way to do it with RegExp.

Here comes a Javascript to detect duplicate points in your string. Make sure it fits:

function DetectarRepeticoes(str) {
    var obj = {};
    for (var itL = 1; itL <= str.length; itL++) {
        for (var itS = 0; itS <= str.length - itL; itS++) {
            var seq = str.substr(itS, itL);
            obj[seq] = obj.hasOwnProperty(seq) ? obj[seq]+1 : 1;
        }
    }
    var res = {};
    for (var k in obj) {
        if (obj.hasOwnProperty(k) && obj[k]>1)
            res[k] = obj[k];
    }
    return res;
}

Example of use:

DetectarRepeticoes("Miguel Angelo Santos Bicudo");

Return:

{
    " ": 3,
    e:   2,
    el:  2,
    g:   2,
    i:   2,
    l:   2,
    n:   2,
    o:   3,
    u:   2
}

Note: this method is order of n2, where n is the number of characters in the string... so it’s not very efficient. But if using small strings is acceptable... up to 1000 characters I think it still looks good in most cases.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.