Jump to content
Sign in to follow this  
paulonline2501

Simple Regex Help

Recommended Posts

hi there, i need a little help with my regex.its been a while since i did this and forgot how to do something quite simple.i want to look for a section of the string that starts in a specific way, has something random in the middle, and ends in a secific way. so for example if i was looking for the the bit that starts 4 and ends 6 in 123456789 i would want to get back 456. the bit in the middle can be any characters and any lenght.i thought i could look for "start"+[.]+"end" but this dfoesnt seem to work. wherethe start is <div class="content">the end is </div> my code so far is below: this ones works if i hard code what im looking for ['stuff to replace']:

$pattern = '/\<div class\=\"content\"\>stuff to replace\<\/div\>/';$replacement = '<div class=\"content\">this has been updated</div>';$updatedData = preg_replace($pattern, $replacement, $theFileData);
the problem is when i start to look for 'anything' between the start and end:
$pattern = '/\<div class\=\"content\"\>+[.]+\<\/div\>/';$replacement = '<div class=\"content\">this has been updated</div>';$updatedData = preg_replace($pattern, $replacement, $theFileData);
please help.

Share this post


Link to post
Share on other sites

...i can get to work for any character but only if there is one character. i need it to look for any character and any number of characters. thats why i put the [brackets] around the. this worked, but only if there is one character:$pattern = '/\<div class\=\"content\"\>.\<\/div\>/'; how do i look for any number of 'any characters'???

Share this post


Link to post
Share on other sites

...it doesnt work if there are spaces or line returns!how can i include these? i literally what to include anything. i tried adding :space: and :punct: but that didnt work or my punctuation is wrong:

$pattern = '/\<div class\=\"content\"\>[.:space::punct:]*\<\/div\>/';

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...