473,386 Members | 1,720 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,386 software developers and data experts.

Non-capturing parentheses

I'll be frank: what is the point? The use, and usefulness, of a set of
capturing parentheses in a regular expression is clear. However, isn't an
expression such as "/(?:x)/", exactly the same as "/x/", or is the former
a better way of including a literal substring within an expression?

I know there must be a valid reason for including them, but I'm not seeing
it at the moment.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #1
6 2547


Michael Winter wrote:
I'll be frank: what is the point? The use, and usefulness, of a set of
capturing parentheses in a regular expression is clear. However, isn't
an expression such as "/(?:x)/", exactly the same as "/x/", or is the
former a better way of including a literal substring within an expression?


Often you use parentheses to group some more complex subexpression e.g.
/Windows\s+(95|98|ME|NT|XP)/
and there you can avoid capturing the value in parentheses if you don't
need it:

var pattern1 = /Windows\s+(95|98|ME|NT|XP)/;
var string = "Windows XP";
alert(pattern1.exec(string))
var pattern2 = /Windows\s+(?:95|98|ME|NT|XP)/;
alert(pattern2.exec(string))

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
On Sun, 18 Jan 2004 16:19:57 +0100, Martin Honnen <ma*******@yahoo.de>
wrote:
Often you use parentheses to group some more complex subexpression e.g.
/Windows\s+(95|98|ME|NT|XP)/
and there you can avoid capturing the value in parentheses if you don't
need it:


I thought that would be a sensible use for them - as evidenced by their
common use in various programming languages - but I don't remember reading
anything about parentheses, of either type, acting as grouping operators
in either Netscape's JavaScript reference (v1.5) or ECMA-262[1].

Mike
[1] I find that document *incredibly* difficult to read. I've read various
standards and specs covering a wide range of topics, from hardware and
drivers to encryption algorithms, but it's the most obscure one yet.

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #3


Michael Winter wrote:
On Sun, 18 Jan 2004 16:19:57 +0100, Martin Honnen <ma*******@yahoo.de>
wrote:
Often you use parentheses to group some more complex subexpression e.g.
/Windows\s+(95|98|ME|NT|XP)/
and there you can avoid capturing the value in parentheses if you
don't need it:

I thought that would be a sensible use for them - as evidenced by their
common use in various programming languages - but I don't remember
reading anything about parentheses, of either type, acting as grouping
operators in either Netscape's JavaScript reference (v1.5) or ECMA-262[1].


As far as ECMAScript edition 3 is your concern then look into section
15.10.1 where the grammar for regular epxression patterns is given, you
will certainly find that the above example is correct syntax.
As for evaluating disjunctions that is explained in section 15.10.2.3.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #4
Michael Winter <M.******@blueyonder.co.invalid> wrote in message news:<op**************@news-text.blueyonder.co.uk>...
I'll be frank: what is the point? The use, and usefulness, of a set of
capturing parentheses in a regular expression is clear. However, isn't an
expression such as "/(?:x)/", exactly the same as "/x/", or is the former
a better way of including a literal substring within an expression?

I know there must be a valid reason for including them, but I'm not seeing
it at the moment.

Mike


Mike,
am really confused by your question. As far as I can see your
first regular expression is eqivalent to
"/({0,1}:x)/"
As the {0,1}, or the ? don't actually follow anything, I'd be
surprised if it was a valid regular expression, and if it was,
something very funny would be going on. If you want to match
"agivenstring", then "/agivenstring/" seems to be a fine way
of doing it.
If you do choose to use parenthesis, then, as you said the
usefulness is obvious, particularly if your using wildcards,
numbers of matches, or searching for a non fixed string. The
parenthesis enable you to retrieve the string that was actually
found.
HTH,
Judas.
Jul 20 '05 #5
js******@sghms.ac.uk (Judas) writes:
am really confused by your question. As far as I can see your
first regular expression is eqivalent to
"/({0,1}:x)/" As the {0,1}, or the ? don't actually follow anything, I'd be
surprised if it was a valid regular expression, and if it was,
something very funny would be going on.


Since you can't write ? (or {0,1}) without a token before, the syntax
"(?" was preciously illegal. That made it a prime candidate use for
when new features were needed.

In new and improved regular expressions, the sequence "(?" starts one of
the new features.
(?: ... ) - is a non-capturing grouping, just as ( ... ) but without
counting as a match
(?= ... ) - is a positive lookeahead. It has zero width but matches
if the following is matched by the expression inside.
(?! ... ) - is the negative lookeahed.
Example:
/^(?=\d{10}$)0*1*$/
This matches a sequence of ten 0's and 1's where all the zeros comes
before the ones. The (?=\d{10}$) matches a zero-width string where
the remainder of the string consists of 10 digits. The 0*1* matches
a string of any length of 0's and 1's where all the 0's comes before
all the 1's.

Example:
/^(?!\d{10}).{10}$/
This matches a string of ten characters that are not all digits.
The (?!\d{10}) matches a zero-width string that is not followed
by ten digit. The .{10} matches any ten characters (except newline).
So, a string is matched only if it is ten characters, and just
before it, the lookahead is not ten digits.

Both examples are of tests that are longer if you need to do them
without lookahead (at least I couldn't find a shorter way, and I'm
pretty good with regular expressions :).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6
rh
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<hd**********@hotpop.com>...
Since you can't write ? (or {0,1}) without a token before, the syntax
"(?" was preciously illegal. That made it a prime candidate use for
when new features were needed.


Well, "can't write" is what I'd expect and, as I read it, what the
ECMA-262/3 syntax specifies. However, both Opera (7.11) and Netscape
(7.1) will each allow:

a = /{0,1}/;

and a number of other variations. So it appears neither excludes an
unescaped "{" as a pattern character. Otherwise, Opera's RegExp
support seems to be very good.

../rh
Jul 20 '05 #7

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

Similar topics

5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.