Search for multiple string occurrence in String via PHP - regex

I am working on an ecommerce website using MVC,php. I have a field called description. The user can enter multiple product id's in the description field.
For example {productID = 34}, {productID = 58}
I am trying to get all product ID's from this field. Just the product ID.
How do i go about this?

This solution doesn't use a capture group. Rather, it uses \K so that the full string elements become what would otherwise be captured using parentheses. This is a good practice because it reduces the array element count by 50%.
$description="{productID = 34}, {productID = 58}";
if(preg_match_all('/productID = \K\d+/',$description,$ids)){
var_export($ids[0]);
}
// output: array(0=>'34',1=>'58')
// \K in the regex means: keep text from this point

Without using regex, something like this should work for returning the string positions:
<code>
$html = "dddasdfdddasdffff";
$needle = "asdf";
$lastPos = 0;
$positions = array();
while (($lastPos = strpos($html, $needle, $lastPos))!== false) {
$positions[] = $lastPos;
$lastPos = $lastPos + strlen($needle);
}
// Displays 3 and 10
foreach ($positions as $value) {
echo $value ."<br />";
}
</code>

Related

How to remove and ID from a string

I have a string that looks like this, they are ids in a table:
1,2,3,4,5,6,7,8,9
If someone deletes something from the database, I will need to update the string. I know that doing this it will remove the value, but not the commas. Any idea how can I check if the id has a comma before and after so my string doesn't break?
$new_values = $original_values[0];
$new_values =~ s/$car_id//;
Result: 1,2,,4,5,6,7,8,9 using the above sample (bad). It should be 1,2,4,5,6,7,8,9.
To remove the $car_id from the string:
my $car_id = 3;
my $new_values = q{1,2,3,4,5,6,7,8,9};
$new_values = join q{,}, grep { $_ != $car_id }
split /,/, $new_values;
say $new_values;
# Prints:
# 1,2,4,5,6,7,8,9
If you already removed the id(s), and you need to remove the extra commas, reformat the string like so:
my $new_values = q{,,1,2,,4,5,6,7,8,9,,,};
$new_values = join q{,}, grep { /\d/ } split /,/, $new_values;
say $new_values;
# Prints:
# 1,2,4,5,6,7,8,9
You can use
s/^$car_id,|,$car_id\b//
Details
^ - start of string
$car_id - variable value
, - comma
| - or
, - comma
$car_id - variable value
\b - word boundary.
s/^\Q$car_id\E,|,\Q$car_id\E\b//
Another approach is to store an extra leading and trailing comma (,1,2,3,4,5,6,7,8,9,)
The main benefit is that it makes it easier to search for the id using SQL (since you can search for ,$car_id,). Same goes for editing it.
On the Perl side, you'd use
s/,\K\Q$car_id\E,// # To remove
substr($_, 1, -1) # To get actual string
Ugly way: use regex to remove the value, then simplify
$new_values = $oringa_value[0];
$new_values =~ s/$car_id//;
$new_values =~ s/,+/,/;
Nice way: split and merge
$new_values = $oringa_value[0];
my #values = split(/,/, $new_values);
my $index = 0;
$index++ until $values[$index] eq $car_id;
splice(#values, $index, 1);
$new_values = join(',', #values);

Regex greedy to pull only required information

I have one scenario
CF-123/NAME-ANUBHAV/RT-INR 450/SI-No smoking/SC-123
Regex should be compatible with java and it needs to be done in one statement.
wherein I have to pick some information from this string.which are prefixed with predefined tags and have to put them in named groups.
(CF-) confirmationNumber = 123
(Name-) name = ANUBHAV
(RT-) rate = INR 450
(SI-) specialInformation = No smoking
(SC-) serviceCode = 123
I have written below regex:
^(CF-(?<confirmationNumber>.*?)(\/|$))?(([^\s]+)(\/|$))?(NAME-(?<name>.*?)(\/|$))?([^\s]+(\/|$))?(RT-(?<rate>.*?)(\/|$))?([^\s]+(\/|$))?(SI-(?<specialInformation>.*?)(\/|$))?([^\s]+(\/|$))?(SC-(?<serviceCode>.*)(\/|$))?
There can be certain scenarios.
**1st:** CF-123/**Ignore**/NAME-ANUBHAV/RT-INR 450/SI-No smoking/SC-123
**2nd:** CF-123//NAME-ANUBHAV/RT-INR 450/SI-No smoking/SC-123
**3rd:** CF-123/NAME-ANUBHAV/RT-INR 450/**Ignore**/SI-No smoking/SC-123
there can be certain tags in between the string separated by / which we don't need to capture in our named group.enter code here
Basically we need to pick CF-,NAME-,RT-,SI-,SC- and have to assign them in confirmationNumber,name,rate,specialInformation,serviceCode. Anything coming in between the string need not to be captured.
To find the five bits of information that you are interested, you can use a pattern with named groups, compiling the pattern with the regex Pattern
Then, you can use the regex Matcher to find groups
String line = "CF-123/**Ignore**/NAME-ANUBHAV/RT-INR 450/SI-No smoking/SC-123";
String pattern = "CF-(?<confirmationNumber>[^/]+).*NAME-(?<name>[^/]+).*RT-(?<rate>[^/]+).*SI-(?<specialInformation>[^/]+).*SC-(?<serviceCode>[^/]+).*";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
After that, you can work with the matched groups:
if (m.find( )) {
String confirmationNumber = m.group("confirmationNumber");
String name = m.group("name");
String rate = m.group("rate");
String specialInformation = m.group("specialInformation");
String serviceCode = m.group("serviceCode");
// continue with your processing
} else {
System.out.println("NO MATCH");
}

Typoscript - Use regex replace on field value

A part of a COA I have gets the page title. What I would like to do is replace the space between the last two words with a
But adding the stdWrap.replacement part results in no output at all:
stdWrap.cObject = COA
stdWrap.cObject {
10 = TEXT
10.field = title
10.htmlSpecialChars = 1
10.wrap = <h2>|</h2>
10.stdWrap.replacement {
10 {
search = \s(\S+)$
replace = \1
useRegExp = 1
}
}
}
Have a look at the Typo3 replacement docs:
10 {
search = #(a )CAT#i
replace = \1cat
useRegExp = 1
}
The # in #(a )CAT#i are regex delimiters. So, all you need is provide any, say:
search = /\s(\S+)$/

Google sheet : REGEXREPLACE match everything except a particular pattern

I would try to replace everything inside this string :
[JGMORGAN - BANK2] n° 10 NEWYORK, n° 222 CAEN, MONTELLIER, VANNES / TARARTA TIs
1303222074, 1403281851 & 1307239335 et Cloture TIs 1403277567,
1410315029
Except the following numbers :
1303222074
1403281851
1307239335
1403277567
1410315029
I have built a REGEX to match them :
1[0-9]{9}
But I have not figured it out to do the opposite that is everything except all matches ...
google spreadsheet use the Re2 regex engine and doesn't support many usefull features that can help you to do that. So a basic workaround can help you:
match what you want to preserve first and capture it:
pattern: [0-9]*(?:[0-9]{0,9}[^0-9]+)*(?:([0-9]{9,})|[0-9]*\z)
replacement: $1 (with a space after)
demo
So probably something like this:
=TRIM(REGEXREPLACE("[JGMORGAN - BANK2] n° 10 NEWYORK, n° 222 CAEN, MONTELLIER, VANNES / TARARTA TIs 1303222074, 1403281851 & 1307239335 et Cloture TIs 1403277567, 1410315029"; "[0-9]*(?:[0-9]{0,9}[^0-9]+)*(?:([0-9]{9,})|[0-9]*\z)"; "$1 "))
You can also do this with dynamic native functions:
=REGEXEXTRACT(A1,rept("(\d{10}).*",counta(split(regexreplace(A1,"\d{10}","#"),"#"))-1))
basically it is first split by the desired string, to figure out how many occurrences there are of it, then repeats the regex to dynamically create that number of capture groups, thus leaving you in the end with only those values.
First of all thank you Casimir for your help. It gave me an idea that will not be possible with a built-in functions and strong regex lol.
I found out that I can make a homemade function for my own purposes (yes I'm not very "up to date").
It's not very well coded and it returns doublons. But rather than fixing it properly, I use the built in UNIQUE() function on top of if to get rid of them; it's ugly and I'm lazy but it does the job, that is, a list of all matches of on specific regex (which is: 1[0-9]{9}). Here it is:
function ti_extract(input) {
var tab_tis = new Array();
var tab_strings = new Array();
tab_tis.push(input.match(/1[0-9]{9}/)); // get the TI and insert in tab_tis
var string_modif = input.replace(tab_tis[0], " "); // modify source string (remove everything except the TI)
tab_strings.push(string_modif); // insert this new string in the table
var v = 0;
var patt = new RegExp(/1[0-9]{9}/);
var fin = patt.test(tab_strings[v]);
var first_string = tab_strings[v];
do {
first_string = tab_strings[v]; // string 0, or the string with the first removed TI
tab_tis.push(first_string.match(/1[0-9]{9}/)); // analyze the string and get the new TI to put it in the table
var string_modif2 = first_string.replace(tab_tis[v], " "); // modify the string again to remove the new TI from the old string
tab_strings.push(string_modif2);
v += 1;
}
while(v < 15)
return tab_tis;
}

Using regular expressions to add numbers using find and replace in Notepad++

I have a SPROC which is having the multiple instances of string Say '#TRML_CLOSE'.
I want to make them to be concatenated with a sequence of numbers.
Eg:
Search and find string '#TRML_CLOSE'
And
Replace the 1st Instance with '#TRML_CLOSE_1',
Replace the 2nd Instance with '#TRML_CLOSE_2',
Replace the 3nd Instance with '#TRML_CLOSE_3',
and so on.
How do I achieve this in Notepad++ using expressions.
I don't know the extent you can script Notepad++, but I do know you can throw together a quick JavaScript snippet to do what you want. http://jsfiddle.net/x4eSr/
Just go to the JS fiddle, and hit the button.
document.getElementById("btn").onclick = function() {
var elm = document.getElementById("txt");
var val = elm.value;
var cnt = 1;
val = val.replace(/#TRML_CLOSE(?!=[_])/g, function(m) {
return m + "_" + cnt++;
});
elm.value = val;
};
Using JavaScript's string.replace(regex, function(){}) which calls the function on each match and a globally incremented "cnt" variable.