Jump to content

Character Class length limit?


mhykhh

Recommended Posts

Hi,

 

I'm a beginner when it comes to regex and I'm stuck with a problem involving the character class. Say I have the following pattern:

/<my-customtag id=([0-9]+)>([^<\/my\-customtag>]+)<\/my-customtag>/

 

The pattern above doesn't product any matches even if the haystack is something like - <my-customtag id=1>Some Text</my-customtag>

 

But when I trim the length of the characters inside the character class like:

/<my-customtag id=([0-9]+)>([^<\/my]+)<\/my-customtag>/

 

It works fine...

 

Any ideas?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/40491-character-class-length-limit/
Share on other sites

A character class allows any character inside to be matched; a better approach is:

 

    <my-customtag id=([0-9]+)>(.+?)<\/my-customtag>

 

...or, if you use a preg shorthand and change the delimiter...

 

    <my-customtag id=(\d+)>(.+?)</my-customtag>

 

If you want to add specific lengths to a character class, use the {min, max} quantifier.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.