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) capture two substrings if delimiter exists, otherwise one substring and blank

I want to use a regex to capture two substrings into two separate capture groups. The delimiter between Country and City is " ■" (a space followed by a black square). If the delimiter doesn’t exist, then it means there is no city, in which case it should capture a blank value. Here is the text:

<p>USA</p>
<p>SPAIN ■Madrid</p>
<p>FRANCE</p>

I have the following regex, which captures everything between the <p> tags:

/<p>(.+)<\/p>/

How can I capture Country and City separately (or blank city if no delimiter)?

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

>Solution :

Try:

/<p>(.+?)(?:\s■(.+))?<\/p>/

(.+?) is the first capture group. The ? makes it lazy to not interfere with the whitespace.
The second group is a non-capture group due to the ?: and it contains the second capture group (.+)
The ? after the non-capture group makes the whole construct optional in case there is no city.

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