473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

grouping regular expression

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.

Dec 8 '06 #1
8 1872
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".

--
Dave Sexton

<sr*****@mailcity.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
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.

Dec 9 '06 #2
sr*****@mailcity.com wrote:
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.
Dec 9 '06 #3
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 Shemitz wrote:
sr*****@mailcity.com wrote:
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.
Dec 9 '06 #4
sr*****@mailcity.com wrote:
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?

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
Dec 9 '06 #5
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
Jon Shemitz wrote:
sr*****@mailcity.com wrote:
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?

--

.NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
Dec 10 '06 #6
sr*****@mailcity.com wrote:
with a regular expression, i would want to have http://www.google.com
as valid and also http:// as valid.
OK.

http:// (?'nondefault' ([\w-]+ \.)* [\w-]+)?

ExplicitCapture | IgnorePatternWhitespace

will match either a bare http:// or a simple URL like
http://www.google.com, and it will capture the www.google.com part in
the "nondefault" group.

--

..NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.
Dec 10 '06 #7
In article <Oc**************@TK2MSFTNGP04.phx.gbl>,
Dave Sexton <dave@jwa[remove.this]online.comwrote:

: 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
--
Last week, for example, Victor David Hansen credits Bush's Iraq war for
"bringing consensual government into the heart of Middle Eastern autocracy."
That's a really stupid way to describe martial law imposed by a foreign
military conquering power . . . -- Jeffrey A. Tucker
Dec 13 '06 #8
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:my****@mysite.com

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

--
Dave Sexton

"Greg Bacon" <gb****@hiwaay.netwrote in message
news:12*************@corp.supernews.com...
In article <Oc**************@TK2MSFTNGP04.phx.gbl>,
Dave Sexton <dave@jwa[remove.this]online.comwrote:

: 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
--
Last week, for example, Victor David Hansen credits Bush's Iraq war for
"bringing consensual government into the heart of Middle Eastern
autocracy."
That's a really stupid way to describe martial law imposed by a foreign
military conquering power . . . -- Jeffrey A. Tucker

Dec 14 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ahaque38 | last post by:
Hello. Using A2K SP3, I am having the following problem with a report using "Sorting and Grouping". I have recently added a grouping in the reports for "Category2<>'CONTRACTS'". I have...
2
by: IKAS | last post by:
I am having some difficulty in using Regular expression in .NET with C#. How do I write a regular expression so that it will match both the following strings and also get me the relevant named...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
8
by: Ahmad A. Rahman | last post by:
Hi all, I have a problem constructing a regular expression using .net. I have a string, separated with comma, and I want to group the string together but, I failed to group a numeric character...
0
by: Laser Lu | last post by:
Hi, all, Does any body know how to use this Grouping Construct? (?> ) The following is its description in the MSDN reference: Nonbacktracking subexpression (also known as a "greedy"...
12
by: Laser Lu | last post by:
Hello, everybody, do you know how to use this Grouping Construct? (?> ) I've found its reference on MSDN, but still can not understand it totally. The following is its description: ...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
5
by: Tom | last post by:
- matches "a", "b", or "c" - matches anything except "a", "b", and "c" (abc) - matches "abc", with back reference ??? - matches anything except "abc" literally?? What is the syntax for this...
6
by: sqlbraindead | last post by:
The problem is if I want to group my database on the time basis, how can reconcile the grouping of two different criteria structure? This is an example. Two table I want to use these to get a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.