Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

RegEx numeric list

He have a text with a numeric list and he would like a regex for have each items in a array.

For give an exemple :

text= "Bien sûr, voici trois idées d’articles qui pourraient être intéressants pour votre site : 1. Les impacts des deepfakes sur la société : une étude sur les conséquences de la manipulation des images et des vidéos dans l’ère numérique. 2. Comment l’IA peut être utilisée pour détecter les fake news sur les réseaux sociaux : une exploration des outils qui utilisent l’apprentissage automatique pour identifier et signaler les fausses informations. 3. Une analyse des tendances de la cybersécurité en 2021 : une vue d’ensemble des menaces les plus importantes pour les entreprises et les particuliers, ainsi que des solutions possibles pour s’en protéger."

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

And i want a array

array = 
[
0==>"Les impacts des deepfakes sur la société : une étude sur les conséquences de la manipulation des images et des vidéos dans l'ère numérique."
1==>"Comment l'IA peut être utilisée pour détecter les fake news sur les réseaux sociaux : une exploration des outils qui utilisent l'apprentissage automatique pour identifier et signaler les fausses informations."
2==>"Une analyse des tendances de la cybersécurité en 2021 : une vue d'ensemble des menaces les plus importantes pour les entreprises et les particuliers, ainsi que des solutions possibles pour s'en protéger."
]

Thanks for your help

I try a lot of regex on https://regex101.com/ but any are good

>Solution :

You could use this regular expression:

(?<=\d\. )(?:(?!\s*\d+\.).)*

If your text can have line breaks within the same "item", then make sure to provide the s flag to the regular expression.

Breaking this down:

  • (?<=\d\. ) asserts that the match starts after a digit+point+space sequence

  • (?!\s*\d+\.) asserts that at the current position we don’t have optional white space followed by a number and a point, as that would indicate we are at the end of the current item.

  • .: any character (expect new line character if the s flag was not provided).

  • (?!\s*\d+\.).: any character that is not beyond the end of a current item (see above)

  • (?:(?!\s*\d+\.).)* matches a series of characters that belong to one item

Depending on the programming language you use, there might be more efficient solutions, like using a split method with a regular expression as argument, or when the language allows to extract the strings that were captured in regex capture groups.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading