# URLPatterns Regex Help

**URL:** <https://forums.plex.tv/t/urlpatterns-regex-help/37627>\
**Category:** Dev/API Corner\
**Tags:** plugin-dev\
**Created:** [July 1, 2013, 11:51pm UTC](https://forums.plex.tv/t/urlpatterns-regex-help/37627 "2013-07-01T23:51:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattrk](https://avatars.discourse-cdn.com/v4/letter/m/4da419/32.png) [@mattrk](https://forums.plex.tv/u/mattrk)\
**Post date:** [July 1, 2013, 11:51pm UTC](https://forums.plex.tv/t/urlpatterns-regex-help/37627/1 "2013-07-01T23:51:13Z")

</div>

I am trying to fix a bug in my ServiceInfo.plist file that is causing some problems. I've discovered some new URLs that aren't getting a match.&nbsp;

&nbsp;

Here are some example of normal episode URLS:

```
http://www.crunchyroll.com/[series_name]/episode-[episode_number]-[episode_name_separated_by_dashes]-[episode_id]

> **[Magi](https://www.crunchyroll.com/magi/episode-15-cassims-answer-612267)**
>
> Watch Magi: The Labyrinth of Magic Episode 15 - Cassim's Answer

> **[Attack on Titan](https://www.crunchyroll.com/attack-on-titan/episode-12-wound-the-battle-for-trost-8-623913)**
>
> Watch Attack on Titan Episode 12 - Wound - The Battle for Trost (8)

> **[Hunter x Hunter](https://www.crunchyroll.com/hunter-x-hunter/episode-85-light-x-and-x-dark-623013)**
>
> Watch Hunter x Hunter Episode 85 - Light X And X Dark
```

&nbsp;

With those URLs in mind, Sander came up with the following regex to match URLs for my service:

&nbsp;

```
^http://www.crunchyroll.com/[^/]+/episode\-\d+\-
```

&nbsp;

Since then i've discovered some new URLs (preview videos, not actual full length episodes) being passed that don't match the above regex. These are the URLs causing my URL service problems:

&nbsp;

```
http://www.crunchyroll.com/[series_name]/[series_name]-pv-[episode_number]-[episode_id]

> **[Magi](https://www.crunchyroll.com/magi/magi-the-labyrinth-of-magic-pv-1-612049)**
>
> Watch Magi: The Labyrinth of Magic - PV 1 - PV 1

[http://www.crunchyroll.com/blast-of-tempest/blast-of-tempest-pv-1-612047](http://www.crunchyroll.com/blast-of-tempest/blast-of-tempest-pv-1-612047)
```

&nbsp;

As i'm terrible at regex, i'm having a bit of trouble coming up with a regex that matches the above URLs. Here is my best guess:

&nbsp;

```
^http://www.crunchyroll.com/[^/]+/.+pv\-\d+\-
```

&nbsp;

Any help would be appreciated!&nbsp;

&nbsp;

Edit: To clarify, the above string does work, but i'm not sure if it's correct. I just want to make sure i'm not casting the net too wide and matching URLs that aren't exactly what i need.&nbsp;

---

<div class="post-metadata">

**Author:** ![mikedm139](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/mikedm139/32/96909_2.png) [@mikedm139](https://forums.plex.tv/u/mikedm139)\
**Post date:** [July 2, 2013, 4:19pm UTC](https://forums.plex.tv/t/urlpatterns-regex-help/37627/2 "2013-07-02T16:19:45Z")

</div>

That looks fine to me, although I might tweak it a little:

```
^http://www.crunchyroll.com/[^/]+/.+?\-pv\-\d+\-\d+
```

I haven't tested this but in theory, the additions will do a few things.

1.) Adding the '?' after the '.+' makes it less "greedy" so it's less likely to match stuff that it shouldn't.

2.) Adding the '\-' before the 'pv' to match the one that follows it, insures that the matches will always include '-pv-' and won't incorrectly match edge cases where there might be a pv in the title or something wacky like that.

3.) Adding the last '\d+' should make sure that the url contains both the episode number and the episode id. It should help avoid situations where malformed urls provide the episode number but not the id which may (or may not) be problematic.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [December 20, 2019, 11:39pm UTC](https://forums.plex.tv/t/urlpatterns-regex-help/37627/3 "2019-12-20T23:39:06Z")

</div>

Closed on December 20, 2019, 11:39pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
