Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8014953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:06:18+00:00 2026-06-04T20:06:18+00:00

I have the following regex to detect start and end script tags in the

  • 0

I have the following regex to detect start and end script tags in the html file:

<script(?:[^<]+|<(?:[^/]|/(?:[^s])))*>(?:[^<]+|<(?:[^/]|/(?:[^s]))*)</script>

meaning in short it will catch: <script "NOT THIS</s" > "NOT THIS</s" </script>

it works but needs really long time to detect <script>,
even minutes or hours for long strings

The lite version works perfectly even for long string:

<script[^<]*>[^<]*</script>

however, the extended pattern I use as well for other tags like <a> where < and > are possible to appears also as values of attributes.

python test:

import re
pattern = re.compile('<script(?:[^<]+|<(?:[^/]|/(?:[^s])))*>(?:[^<]+|<(?:[^/]|/(?:^s]))*)</script>', re.I + re.DOTALL)
re.search(pattern, '11<script type="text/javascript"> easy>example</script>22').group()
re.search(pattern, '<script type="text/javascript">' + ('hard example' * 50) + '</script>').group()

how can I fix it?
The inner part of regex (after <script>) should be changed and simplified.

PS 🙂 Anticipate your answers about the wrong approach like using regex in html parsing,
I know very well many html/xml parsers, and what I can expect in often broken html code, and regex is really useful here.

comment:
well, I need to handle:
each <a < document like this.border="5px;">
and approach is to use parsers and regex together
BeautifulSoup is only 2k lines, which not handling every html and just extends regex from sgmllib.

and the main reason is that I must know exact the position where every tag starts and stop. and every broken html must be handled.

BS is not perfect, sometimes happens:
BeautifulSoup(‘< scriPt\n\n>a<aa>s< /script>’).findAll(‘script’) == []

@Cylian:
atomic grouping as you know is not available in python’s re.
so non-geedy everything .*? until <\s/\stag\s*>** is a winner at this time.

I know that is not perfect in that case:
re.search(‘<\sscript.?<\s*/\sscript\s>’,'< script </script> shit </script>’).group()
but I can handle refused tail in the next parsing.

It’s pretty obvious that html parsing with regex is not one battle figthing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:06:20+00:00Added an answer on June 4, 2026 at 8:06 pm

    I don’t know python, but I know regular expressions:

    if you use the greedy/non-greedy operators you get a much simpler regex:

    <script.*?>.*?</script>

    This is assuming there are no nested scripts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following regex to detect URLS: /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig However, it doesn't detect urls
I have written the following regex to match a set of e-mails from HTML
I have the following data: abc def; ghi. This regex will match: ([a-z0-9A-ZÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ ]*)\W
I have been trying to write a regex that will remove whitespace following a
I have the following regex to detect text inside <?php (including the tag) '/<\?php(.*)\?>/isU'
I have the following regex expression to match html links: <a\s*href=['|](http:\/\/(.*?)\S['|]> it kind of
I have the following regex in a PHP script $total_matches = preg_match_all('{ <a\shref= (?<link>[^]+)
I have the following Regex to match all link tags on a page generated
I have following regex (abc|def)( ?(\\d+|(?:(?!\\1)[a-z])+)?)* with matches perfectly the subject abc123 456 .
I might have pretty basic question about regex. I have the following regex, which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.