N Kumar
N Kumar

Reputation: 1312

Regex get last word of pattern

i have this string

<p>&nbsp;&nbsp;&nbsp;&nbsp;this is some text</p>

&nbsp; can be any number of times

to match i am using regex (?<=<p.*?>*&nbsp;)(.*)(?=</p>)

but i am getting &nbsp;&nbsp;&nbsp;this is some text as output

How to get this is some text

EDIT

i am sorry my string is <p class='randomstring'>a)&nbsp;&nbsp;&nbsp;&nbsp;this is some text</p> in place of a) there is digit some times.

Upvotes: 0

Views: 279

Answers (2)

anubhava
anubhava

Reputation: 785156

You can use this regex:

(?<=<p[^>]*>)(?:&nbsp;)+(.*)(?=</p>)

And grab the captured group #1 for you match, that will be:

this is some text

EDIT: Based on your edited question try this regex:

(?<=<p[^>]*>)[^)]*\) *(?:&nbsp;)+(.*)(?=</p>)

Upvotes: 3

Avinash Raj
Avinash Raj

Reputation: 174706

You could use the below regex which uses variable length positive lookbehind.

(?<=<p[^>]*>(?:&nbsp;)+)\b.*?(?=</p>)

This should match only the string this is some text

Update:

(?<=<p[^>]*>\w*\)(?:&nbsp;)+)\b.*?(?=</p>)

Upvotes: 2

Related Questions