Viren
Viren

Reputation: 1

perl regex match from last, skipping the last delimeter

I am trying to write the Regex in perl for the pattern:

""Wagner JS, Adson MA, Van Heerden JA et al (1984) The natural history of hepatic metastases from colorectal cancer. A comparison with resective treatment. Ann Surg 199:502–508""\s

to get the last part: "Ann Surg 199:502–508" SO I wrote

$string =~ m/\.([^\d]*\s\d*\:\d*\–\d*)\"\"\s$/

The match part I am getting in $1 is: "A comparison with resective treatment. Ann Surg 199:502–508" but I am expecting: "Ann Surg 199:502–508".

In some of the cases it is working but in some of them it is not. Tried searching but didn't get satisfactory answer. Please suggest something.

Upvotes: 0

Views: 50

Answers (4)

Miller
Miller

Reputation: 35198

Another option, taking everything after the last period excluding leading spaces:

$string =~ m/(?!\s)([^.]+)$/

Upvotes: 0

Borodin
Borodin

Reputation: 126722

If you want the last part of every string, then all you need is

$string =~ /([^.]+)$/

or, to avoid the space after the full stop

$string =~ /([^.\s][^.]+)$/

Upvotes: 1

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89547

You only need to add the dot in the character class:

$string =~ m/\.([^\d.]*\s\d*:\d*–\d*)""\\s$/

But a better way is to split the string with dot as delimiter and take the last part.

Upvotes: 1

Lee Duhem
Lee Duhem

Reputation: 15121

Please give this a try:

$string =~ m/\.\s*([^\.\d]*\s*\d*\:\d*\–\d*)""\\s$/;

Upvotes: 0

Related Questions