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

Get pattern depends of input string

Is it possible, and how should regex look like to get everything after the closing bracket or get everything if there are no brackets at all?

example:

possible input 1:

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

[12,45] some text

possible input 2:
some text

expected to get:

 some text

I found something like lookbehing conditional, and tried:

(?(?<=\])((?<=\])(.*))|(.*))

but didn’t work.

This works for the input with brackets:

(?<=\])(.*)

And this works for input without brackets:

(.*)

but is it possible to get one expression to match both input cases?

>Solution :

This regex, you need OR aka |:

(?<=]\s|^)[\w\s]+
#      ^ 
#      |
#    there

The regular expression matches as follows:

Node Explanation
(?<= look behind to see if there is:
] ]
\s whitespace (\n, \r, \t, \f, and " ")
| OR
^ the beginning of the string
) end of look-behind
[\w\s]+ any character of: word characters (a-z, A- Z, 0-9, _), whitespace (\n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible))

Online Demo

regex101

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