Forums Register Login

What ?: does in the regular expression (?:\w*?)?

+Pie Number of slices to send: Send
(?:\w*?)?

What ?: does in the regular expression?

Thanks

+Pie Number of slices to send: Send
Bruce:

Look here.

John.
+Pie Number of slices to send: Send
Thanks John for the link.

It says:

Special constructs (non-capturing)
(?:X) X, as a non-capturing group

What does this mean?

What is "a non-capturing group" and what is "a capturing group"?

Thanks

+Pie Number of slices to send: Send
 

Bruce Jin wrote:
What does this mean?

What is "a non-capturing group" and what is "a capturing group"?



A group is, by default, capturing -- meaning you can fetch groups (sub-matches inside parens) with the group(int) method.

A non-capturing group is just that -- don't capture the submatch. The group(int) method doesn't return submatches from non-capturing groups. Generally, if you don't need the value of a submatch, the group should be non-capturing, as there is no reason for the regex engine to collect the data for you, if you don't intend to use it.

Henry
+Pie Number of slices to send: Send
Thanks Henry for the explanation.
It appears that we should use ?: more often.

Is there a simple example that can show the difference between (?:\w*)? and (\w*)?

Thanks
+Pie Number of slices to send: Send
Just try this with both regular expressions, and see what it returns.
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 12118 times.
Similar Threads
Why are they called Regular Expressions
what is the meaning of below regular expression
regular expression to Remove field inside [ ] brackets
Regular Expression issue
problem in understandin the code
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:04:41.