tags:

views:

75

answers:

5

need help to create regular expression matching string

www.*.abc.*/somestring

Here * is wild card it can be anyhing like us, uk

or com, edu

like

 www.us.abc.com/somestring
www.uk.abc.edu/somestring
+1  A: 
www\.([a-z]{2})\.abc\.(com|edu)/(.+)

You can then extends this regex to include other valid generic top-level domain name (net, org, ...)

www\.([a-z]{2})\.abc\.(com|edu|org|net)/(.+)

You will get the Country code top-level domain in the group number 1, the top-level domain in group 2.

VonC
A: 

If it's a URL, it can match any a-z character, or 0-9, or a dash (-). Each component has at least one character, so use + as the multiplier. Currently, tlds are only a-z, but this regex is a bit more robust (you never know!):

/www\.[-a-z0-9]+?\.abc\.[-a-z0-9]+?\/somestring/

It assumes nothing about the length of each component, and that it's all lowercase.

Jeremy Smyth
You are right, but what if you want to limit yourself to the country codes or top level domain names (and only those ones)? [az] would suffice.
VonC
[a-z], but otherwise you're right. I mention that in my answer anyway :)
Jeremy Smyth
+5  A: 

Put [^.]+ instead of asterisks and \. instead of dots, and you'll be done.

www\.[^.]+\.abc\.[^.]+/somestring

[^.] matches any non-dot, [^.]+ matches a string of nondots with at least one character. \. matches a dot, because . matches any character.

liori
A: 

Try the following regular expression:

^www\.\w+\.abc\.\w+\/\w+$
Alan Haggai Alavi
A: 

Something like that:

www\.\w+\.abc\.\w+\/somestring

I do recommend to use this Online RegEx builder to learn how it works

Bogdan_Ch