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

Split string to groups with regex

I had 6 strings:
[db1 db2] /my/first/path/ /second/optional/path/
[db1 db2] /my/first/path/
--all /my/first/path/ /second/optional/path/
--all /my/first/path/
db /my/first/path/ /second/optional/path/
db /my/first/path/

Is it possible to create regex to split each of this strings to 2 groups (or 3 groups if optional path is added)

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 using the following regex:

^([^\/]+) (\/\S+) ?(\/.+)?

Explanation:

  • ^: the start of string symbol
  • ([^\/]+): Group 1, composed of any character other than a slash
  • : a space
  • (\/\S+): Group 2, composed of a slash and any non space character
  • ?: an optional space
  • (\/.+)?: Group 3 (optional), composed of what remains of the string (any character)
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