grouping regular expression

  • Thread starter Thread starter sri_san
  • Start date Start date
S

sri_san

Hello,
I am trying to write a regular expresssion which validates
against the URL and the textfield has 'http://' prefilled. I either
want a valid URL or just leave the 'http://' as it is. I figured an
expression for URL which is --- "http(s)?://([\w-]+\.)+[\w-]+(/[\w-
../?%&=]*)?"
I am unable to group it based on the above condition. Any help would be
great!

Thanks,
Sam.
 
Hi Sam,

There really is no absolutely flawless way of parsing a URL using a Regex,
IIRC. I believe it's because there are so many possibilities for what makes
a url valid.

I usually try to parse a url by passing it to the constructor of System.Uri.

Then, you can access the different parts programmatically through different
properties and methods, without the use of "grouping".
 
I am trying to write a regular expresssion which validates
against the URL and the textfield has 'http://' prefilled. I either
want a valid URL or just leave the 'http://' as it is. I figured an
expression for URL which is --- "http(s)?://([\w-]+\.)+[\w-]+(/[\w-
./?%&=]*)?"
I am unable to group it based on the above condition. Any help would be
great!

As I understand your question, you want to be able to match both
www.midnightbeach.com and http://www.midnightbeach.com, and have the
regex automatically add the http:// to the first match.

I don't believe you can actually do this. However, you can write a
regex that will match a full or partial URL, and will group the part
after the "http://". Then, you can read the group, knowing it will
never capture "http://", and can thus simply always add "http://" to
the capture.
 
I apologize for the confusion.. let me clarify the issue.. I have a
textbox prefilled with http://
And, I am looking for a regular expression which is a valid URL like
http://www.google.com or just leave http:// in the textbox.

Thanks,
Sam.

Jon said:
I am trying to write a regular expresssion which validates
against the URL and the textfield has 'http://' prefilled. I either
want a valid URL or just leave the 'http://' as it is. I figured an
expression for URL which is --- "http(s)?://([\w-]+\.)+[\w-]+(/[\w-
./?%&=]*)?"
I am unable to group it based on the above condition. Any help would be
great!

As I understand your question, you want to be able to match both
www.midnightbeach.com and http://www.midnightbeach.com, and have the
regex automatically add the http:// to the first match.

I don't believe you can actually do this. However, you can write a
regex that will match a full or partial URL, and will group the part
after the "http://". Then, you can read the group, knowing it will
never capture "http://", and can thus simply always add "http://" to
the capture.

--

.NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
 
I apologize for the confusion.. let me clarify the issue.. I have a
textbox prefilled with http://
And, I am looking for a regular expression which is a valid URL like
http://www.google.com or just leave http:// in the textbox.

Sorry, I don't understand the clarification. Perhaps you could give
some examples?
 
I have a form which has a textbox for the user to input the URL and it
is prefilled with http://. Now, the regular expression has to validate
the url based on the expression from the previous post if the user
enters the url or accept the default value (http://) as a valid value.

Say txtURL has http://

with a regular expression, i would want to have http://www.google.com
as valid and also http:// as valid.

Thanks,
Sam
 
: There really is no absolutely flawless way of parsing a URL using a
: Regex, IIRC. I believe it's because there are so many possibilities
: for what makes a url valid.
: [...]

See Appendix B of RFC 2396: http://www.ietf.org/rfc/rfc2396.txt

Hope this helps,
Greg
 
Hi Greg,

Thanks, that's a good expression to keep on hand.

^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?

Although it isn't complete since it only handles the RFC-compliant URIs.
For example, it won't handle the following which works in Windows Explorer
and IE:

ftp://me:[email protected]

I use the syntax above from time to time. IE7 no longer supports that
syntax for the http protocol, but IE3+ did.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top