Remove number sequence of string

3

I have the following problem, I believe I can solve it with regular expressions, but I'm bad with it.

Let's say I have a string like this: CONECTOR BNC FEMEA + F MACHO 024267 or so PLUG P-4 UNIVERSAL 006-096 , the position of the numbers can be anywhere, beginning, middle or end, I need to take the number sequences that are equal or greater than 3 digits .

These digits are usually for reference and I am using this information to fetch images using the Google Custom Search API, if they are passed to perform the search, it generally does not return any results.

Desired result: Entry: CONECTOR BNC FEMEA + F MACHO 024267 Output: CONECTOR BNC FEMEA + F MACHO

    
asked by anonymous 13.02.2017 / 12:14

1 answer

4

To capture / replace three or more consecutive digits use the regex \d{3,} . \d means numbers (0-9), keys work as quantifiers in case they require three digits or more to be captured.

$str = 'CONECTOR BNC FEMEA + F MACHO 024267 23';
$nova = preg_replace('/\d{3,}/', '', $str);
echo $nova;

Result:

CONECTOR BNC FEMEA + F MACHO 23
    
13.02.2017 / 12:17
___ ___ verify erkimt repeated within the array number C # ______ qstntxt ___

I've created a pre-defined array with some numbers, and I want to know how many repeated numbers it has in the array. but it is only returning the size of the array and not qtd of repeated numbers. and also wanted to remove the repeated numbers.

%pre%     
______ azszpr271810 ___

Assign your array inside a HashSet, as it does not accept repeated values, and then you compare the sizes of both and know how many values were repeated:

%pre%     
______ azszpr271881 ___

As commented,

  

Manieiro :   Set "how many numbers repeated", this can be interpreted as   various forms.

The proposed answer presents a solution that depends exactly on which question is being asked.

  

Gabriel Coletta

%pre%

The result is 5 , a result of the original size difference of %code% subtracted from its %code% , which can also be written as %code%

%pre%

However, we come to a subjective point, 5 actually represents the number of repetitions of the values after their first occurrences, in other words, 5 is equal amount of duplicates or recurrences.

But when we look at the original set %code% , we can see that it has 8 elements of repeated values (which have more than one occurrence). What can be seen more easily by rearranging and removing unique occurrences %code% . And this difference greatly impacts the purpose of the query and what you intend to do with that result.

But the number of repeated numbers can also be aimed at knowing the number of numbers that have had more than one occurrence in the %code% case.

Below I leave a sample script only to demonstrate how I could respond to variations in interpretation according to the specified goal, using some lists to facilitate manipulation and a single %code% .

%pre%     
______ azszpr271818 ___

You are always comparing two equal numbers so the counter ultimately has the same length value. If you do not want to change the structure of your code, I suggest making these changes:

%pre%     
______ azszpr271812 ___

You're making two errors in your code:

The first is that the loop is checking its own number, ie when i and j are the same it compares the same number. The second error is break, finding a repeated number should not break the loop.

%pre%     
______ azszpr271824 ___

If you want to use you can also extract the two information:

Find the items and do not bring the replicates:

%pre%

Group the items and count the number of replicates:

%pre%

See working at dotnetfiddle.net

    
______ azszpr271835 ___

Actually you are comparing the number of the repeat loop and not the array int [] array1 , you have to store the value of the array in a variable, and within the if

  •     
    ___ Python - Problem when doing print within a loop