YadirHB
YadirHB

Reputation: 168

Specific pattern to match and extract info from string

I need to be able identifying over a string if it matches with the following patterns:

':module-name' || 'com.example.ClassName' || ':module-name:com.example.ClassName' || 'http://url';

I need to identify if the string matches one of the above patterns, lets split them into 3 groups (urls, local & none) where a url stands for a valid url string. In the other hand local must be in 3 different variants (':module-name' || 'com.example.ClassName' || ':module-name:com.example.ClassName') if this is the case I need to extract the module and component names.

The module name must be a valid nodejs package name and the component name must be a valid Class name (I got the valid class name already covered with this RegExp: /^@?[a-z_A-Z]\w+(?:.@?[a-z_A-Z]\w+)*$/).

What I need is a way to identify where the string belongs to one of the groups (url, local or none) and in case of local, extract the module and component name. The best for a regular expression solution...

Thanks

Upvotes: 1

Views: 168

Answers (2)

YadirHB
YadirHB

Reputation: 168

By using the amazing web tool referenced by Croutonix I was able to produce the RegExp and test it real time:

The solution is /^:([a-z][a-z_-])+:{0,1}([a-z_A-Z]\w+(?:.?[a-z_A-Z]\w+))$/gm.exec(string)

Upvotes: 0

Nicolas
Nicolas

Reputation: 7111

Can this work? See on regex101

^:.+-.+:.+\..+\..+|^:.+-.+|^.+\..+\..+|^http[s]?:\/\/.+

It matches :x-x, x.x.x, :x-x:x.x.x, http[s]://x, x being any character repeated between 1 and unlimited times. It won't match any of those if preceded by something. But it will match com.class.class.class.class and http://invalidUrl. As you said you check if class name is good. If you want only valid url, then you can replace ^http[s]?:\/\/.+ with https?:\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))? or any regex validating urls. (I did not invent this one)

Upvotes: 1

Related Questions