Extract part of string (Regex)

Hi,

Can anyone show me how i can extract the following using regex?

My string is "TITLE PART 1/13"

I'm looking to extract "PART 1/13".

This wont always be at the end of the string as the string will change, for example it could show as "TITLE PART 1/13 MORE TEXT"

Regards,

Paul
m0tSiEAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ygouthamCommented:
echo "TITLE PART 1/13 MORE TEXT some random text that comes here" | cut -d" " -f2-100

that should be all
0
m0tSiEAuthor Commented:
It shows the error:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in regex.php  on line 3
0
ygouthamCommented:
sorry the above was a linux command, did not see the php portion of it


$searchstring = "TITLE PART 1/13 MORE TEXT some random text that comes here";

$searchpos = strpos($searchstring, " ");

$resultstring = substr($searchstring, $searchpos+1, strlen($searchstring) );

echo "$resultstring";
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

m0tSiEAuthor Commented:
Thanks, I tried it, but it doesn't work. By just searching for the first space as the title wont always be 1 word and i'm only after "Part x/xx", not the text after that.
0
ygouthamCommented:
would the word "PART" be a constant then we can use that to find the string

$searchstring = "TITLE PART 1/13 MORE TEXT some random text that comes here";

$searchpos = strpos($searchstring, "PART");

$resultstring = substr($searchstring, $searchpos, strlen($searchstring) );

echo "$resultstring";

0
ygouthamCommented:

if you want only PART XX/XX


$searchstring = "TITLE PART 1/13 MORE TEXT some random text that comes here";

$searchpos = strpos($searchstring, "PART");

$resultstring = substr($searchstring, $searchpos, strlen($searchstring) );

$resultarray = explode(" ", $resultstring);

$displaystring = $resultarray[0] . " " . $resultarray[1];

echo "$displaystring";

0
theodorejsalvoCommented:
Here is a regex solution.
$oldStr = "TITLE PART 1/13 MORE TEXT some random text that comes here";
$matched = preg_match('@PART \d+/\d+@', $oldStr, $newStr_temp);
$newStr = $newStr_temp[0];

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Shahan AyyubSenior Software EngineerCommented:
Try the below expression :

expression: PART \d\/\d{2}

write this expression like /expression/ , when used.

I 've tested it from here also
:
http://regex.larsolavtorvik.com/

0
Shahan AyyubSenior Software EngineerCommented:
Paul, you can use this Regex, if your denominator length > 2 digits.

PART \d\/\d+

otherwise the previousone would be enough, i.e.,

PART \d\/\d{2}


-Shahan
0
theodorejsalvoCommented:
FYI: I used +'s on both sides to allow for the match "PART 24/325"
0
Terry WoodsIT GuruCommented:
Theodorejsalvo's solution looks perfect to me.
0
spoolerzCommented:
Works fine in Python, take in mind that the regular expression is:

(TITLE\s+PART\s+\d+/\d+).*

>>> import re
>>> s = "TITLE PART 1/13 OTHER TEXT"
>>> regex = re.compile( r"(TITLE\s+PART\s+\d+/\d+).*", re.I )
>>> match = regex.search( s )
>>> print match.group( 1 )
TITLE PART 1/13
0
m0tSiEAuthor Commented:
Thanks All :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.