• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 741
  • Last Modified:

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
0
m0tSiE
Asked:
m0tSiE
  • 4
  • 3
  • 2
  • +3
3 Solutions
 
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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
 
Shahan AyyubSenior Software Engineer - iOSCommented:
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 Engineer - iOSCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now