Last Updated 11 months ago
AAaa Regular expressions (regex) in PrestaShop Importer - examples
Regular expressions (regex) are a huge potential to support the processing of any short and long content. Once mastered, they will be useful to you in many situations in life.
- PrestaShop Importer module supports regular expressions.
- You can use regular expressions on almost any field you want to import or update from XML, CSV or API to PrestaShop.
- With regular expressions, you can modify or process the data in the file to suit any needs before it reaches your store.
Below is a list of sample problems to solve with content found in product feeds. We also provide ready-made regular expressions, with the help of which you will modify the content or retrieve only specific fragments from it.
---------------------
Example 1
Content in the file:
Capacity: 4400 mAh;Voltage: 10.8V (11.1V);Number of cells: 6;Cell type: Li-Ion;Warranty: 12 months;Cell manufacturer: Green Cell;Color: Black;Deep discharge protection: Yes;Overcharge protection: Yes;Product code: HP01;Manufacturer: Green Cell;Dimensions: 205x52x21mm;Weight: 300g ;Compatibility list: ;Replaces models.
Problem:
For the short description wants to give everything before the text ";compatibility list".
Solution:
Regular expression:
/^(.+);Compatibility list/.
Replacement:
$1
---------------------
Example 2
Content in file:
EXHAUST GAS TEMPERATURE SENSOR OPEL ASTRA H 1.3CDTI 2005-,CORSA C 1.3CDTI 2005-,MERIVA 1.3CDTI 2003-/OVAL-BEHIND PARTICULATE FILTER/.
Problem:
The product name is too long, shorten it to a maximum of 120 characters so as to cut off the word at the end of the downloaded text.
Solution:
Regular expression:
/(^.{0,120}($|[\s]+)).*/
Replacement:
$1
---------------------
Example 3
Content in the file:
image
Problem:
The features are given according to the scheme:
Name: value
How to retrieve only the name of the feature?
Solution:
Regular expression:
/^([^:]+)+\s*:\s*(.+)$/
Replacement:
$1
---------------------
Example 4
Content in file:
1,00
Problem:
The quantity available in stock is given as a floating point number instead of an integer.
How to remove unnecessary characters i.e., comma and digits after it?
Solution:
Regular expression:
/,[0-9]+$/
Replacement:
leave the field blank
---------------------
Example 5
Content on file:
Tefal meat grinder NE113135
Problem:
I would like to make comma separated tags from the product name.
How do I replace spaces between words with commas?
Solution:
Regular expression:
/\s/
Replacement:
,
---------------------
Example 6
Content on file:
12,435345
12.56456
Problem:
How to limit the imported number to a maximum of two decimal places?
Solution:
Regular expression:
/^([0-9]+[,.]{1}[0-9]{2}).*$/
Replacement:
$1
---------------------
Example 7
Content in file:
[blank].
Problem:
How to replace the empty content with another content such as the number 100?
Solution:
Regular expression:
/^\s*$/
Replacement:
100