Regex issue with max characters

Ok, the following code works for me the way it should, retrieving the info I want. But, I’m not able to figure out how I limit the number of characters to 50.
Help greatly appreciated :slight_smile:

^(?<Produktnavn>(.|\
|\\r)*)$

Haven’t tried:


^[(?<Produktnavn>(.|n|r)*)]{0,50}$

Thanks, but don’t work I’m afraid…

I am not regex expert though but will still try to find out the solution or maybe others can help you if you can put detailed requirement with example.

I see some of my error here, what I think your code does is retrieve everything that is between 0 and 50 characters isn’t it?
What I’m trying to do is retrieve the entire title, then trim it down to max 60 characters.

If your previous regex retrieves everything as you expected then you can use substr function retrieve required number of characters then. Sorry if I misunderstood! Once again can you paste your code that you have so far with sample input texts?

^(?<Produktnavn>(?:.|[nr]){0,50})$

Why not trim the string down with substr() before or after your regex? It’s simple enough and will not pad your string out to 60 characters:

$str = substr( $str, 0, 60 );

I’m using a program called Mozenda, a webscraper and don’t have access to any more code… Also, I can’t use substr etc, only regex. So, not sure if what I want even is possible…?

as logic stated, turn your * into a limited field (If your 50 characters was to include the <Produktnavn> though, it should be {0,37} )

Thanks guys, made it work, this is the end code, had to remove the $ at the end to make it work:

^(?<Produktnavn>(?:.|[nr]){0,59})

(?P<Produktnavn>pattern_goes_here) is a group name. So when preg_match returns you can access the group via a name instead of a number. $m['Produktnavn']