You can use regex look-around assertions and negated character classes to achieve this.

use strict; use warnings; my $rxTitle = qr {(?x) # Use extended syntax (?<=-\s) # If preceded by hyphen & space ( # Open capture [^(-]+ # One or more non- opening parentheses # or hyphens (?: # Non-capture group for quantifier [^)]+ # One or more non- closing parentheses \) # Followed by a closing parenthesis )? # Quantify, zero or one of ) # Close capture (?=\s?</title>) # If followed by optional space and # closing title tag }; while ( <DATA> ) { my ( $text ) = m{$rxTitle}; print qq{$text\n}; } __END__ <title>GRP -134 - Grinding And Cutting Solution (ACME PR50 - Water Typ +e) </title> <title> GRP-123-1 - Grinding And Cutting Solution (Quakeroat 2780 UTC +- Synthetic Type)</title> <title> GRP-124 - Alkaline Rust Remover Solution</title> <title> GRP-124-1 - Alkaline Rust Remover Solution (Ardvark 185 - Rust + Remover)</title> <title> GRP-124-2 - Alkaline Rust Remover Solution (Ardvark 185L - Rus +t Remover)</title> <title> GRP-124-3 - Alkaline Rust Remover Solution (Bee-Dee J84AL - Ru +st Remover)</title> <title> GRP-124-4 - Alkaline Rust Remover Solution (Mag HD2-202 - Rust + Remover)</title> <title> GRP-124-5 - Alkaline Rust Remover Solution (Turk 4181L - Rust +Remover)</title> <title> GRP-124-6 - Alkaline Rust Remover Solution (Turk 4181 - Rust R +emover)</title> <title> GRP-124-7 - Alkaline Rust Remover Solution (Bee-Dee J84A - Rus +t Remover)</title> <title> GRP-124-8 - Alkaline Rust Remover Solution (Cadilac HTP-1150 - + Rust Remover)</title> <title> GRP-124-9 - Alkaline Rust Remover Solution (Cadilac HTP-1150L +- Rust Remover)</title> <title> GRP-124-10 - Alkaline Rust Remover (Titanium Long Soak)</title +>

The output.

Grinding And Cutting Solution (ACME PR50 - Water Type) Grinding And Cutting Solution (Quakeroat 2780 UTC - Synthetic Typ Alkaline Rust Remover Solution Alkaline Rust Remover Solution (Ardvark 185 - Rust Remover) Alkaline Rust Remover Solution (Ardvark 185L - Rust Remover) Alkaline Rust Remover Solution (Bee-Dee J84AL - Rust Remover) Alkaline Rust Remover Solution (Mag HD2-202 - Rust Remover) Alkaline Rust Remover Solution (Turk 4181L - Rust Remover) Alkaline Rust Remover Solution (Turk 4181 - Rust Remover) Alkaline Rust Remover Solution (Bee-Dee J84A - Rust Remover) Alkaline Rust Remover Solution (Cadilac HTP-1150 - Rust Remover) Alkaline Rust Remover Solution (Cadilac HTP-1150L - Rust Remover) Alkaline Rust Remover (Titanium Long Soak)

I hope this is useful.

Cheers,

JohnGG


In reply to Re: Regex only returning partial data by johngg
in thread Regex only returning partial data by Grey Fox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.