Jump to content

?: in regex


HardCoreMore

Recommended Posts

Hi guys,

 

I started to learn regex and was doing just fine until i saw (?:[A-Z0-9-]+\.)

I just can't find anywhere what ?: does in regex.

 

More correctly  this two patterns

 

1. This one is for email [A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}

In this one the part with ?: should prevent users entering email like john@aol...com.

 

2. Second one is /^(?:br|frame|hr|img|input|link|meta|range|spacer|wbr|area|param|col)$/i

 

I know that ? is used to match 0 or 1 times but don't know what does ?: is for and is that only to be used inside ()

 

Thanks

Link to comment
Share on other sites

  • 3 weeks later...

Hi Guys,

 

@requinix, @salathe

Thanks for your reply.

 

@salathe

I am reading it second time already. I understand it for the most part but not quite complete yet. Probably will read it one more time to understand it completely. But there is something they said there that i don't know the reason for.

 

Qute

 

"The maximum number of captured substrings is 99, and the maximum number of all subpatterns, both capturing and non-capturing, is 200. "

 

 

Does anyone knows why is there a limit to number of captured substrings  and subpattenrs?

 

What i have a really big text file or multiple text files combined that i want to test regex on?

 

 

Thanks! :)

 

 

Link to comment
Share on other sites

I have never in my life ever seen someone need to use more than 99 groups, or even 50...or even 10.  Most patterns use 0-5 groups.  Beyond that, it is usually far more beneficial to be breaking down the data (with or without regex) into smaller chunks of data to work with...or going back and rethinking how you are storing that data in the first place..  I have once or twice seen a regex that used upwards of 10 and one time I think I used almost 20 groups for a particularly complex pattern for someone (it was parsing some custom serialized multi-dim array, matching only certain areas of the subject if other areas matched, etc.. ) but TBH it was more out of me trying to be cool and get all the data out in one regex than doing things the "better" way in the first place (particularly pushing back and saying 'store your shit better').  IOW, if you really think you need anywhere near that many captured groups, the chances are overwhelmingly likely that you are going about storing the data and/or approaching the regex in the wrong way.  I have yet to see any evidence from real world examples/problems where you would actually ever need anywhere near that many groups. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.