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 7758461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:21:59+00:00 2026-06-01T13:21:59+00:00

I was looking at a tutorial on Regex. It was about how to get

  • 0

I was looking at a tutorial on Regex.

It was about how to get the class attribute from this piece of html

<pre class="ruby" name="code">

and the regex used was

<pre class="([^"]+)" name="code">

They recommended to use the above one instead of

<pre class="(.+)" name="code">

“as it goes beyond the quote.”

I don’t understand what they mean. It is just going to work anyway but then why the first regex is recommended. Am I missing anything? Please enlighten me.

Thanks in advance.

  • 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-01T13:22:01+00:00Added an answer on June 1, 2026 at 1:22 pm

    .+ matches greedily. for example, in

    <pre class="ruby" size="medium" name="code"> 
    

    it would match ruby" size="medium. Even worse, if you had two tags on the same line, it would match right across the tag boundaries:

    <pre class="ruby" name="code">foo</pre> <pre class="python" name="code">bar</pre>
    

    would result in ruby" name="code">foo</pre> <pre class="python!

    So as long as you know exactly what your HTML will look like, .+ can work, but as soon as it changes unexpectedly (as HTML is wont to do), your regex wouldn’t simply fail (as the second one would) but it would match the wrong stuff.

    Therefore, the second regex is safer (since it’s more explicit about what exactly is allowed to match). You usually should try and avoid the simple .+ or .* “match anything”, and instead think about what you do want to match.

    That said, for precisely the same reasons, you shouldn’t try and match HTML and other markup languages with regexes anyway because there are better tools for that.

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

Sidebar

Related Questions

i was looking at a tutorial from ZendCasts where i wondered about the code
I am looking at this tutorial and it confuses me as I don't get
I am looking at this tutorial http://www.codeproject.com/KB/cpp/authforwebservices.aspx and I am wondering what the reason
I'm looking for good tutorial or really simple code to integrate mongokit and django
I am looking for a tutorial or example code that will help me understand
I am looking for a tutorial that shows how to populate a UITableView from
Ok, so i was looking this tutorial in codeproject website and in the video
After looking at this tutorial on blobs: channel 9 , I was thinking of
I'm looking for a tutorial on Nhibernate 3.2 that uses the built-in code configuration
I was looking at a tutorial, this one to be exact - http://android-er.blogspot.com/2011/01/start-camera-auto-focusing-autofocus.html And

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.