Jump to content

Simple Regex Help


paulonline2501

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.
Link to comment
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'???

Link to comment
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\>/';
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...