aolong Posted June 2, 2009 Share Posted June 2, 2009 I'm having trouble with this one... I need to replace everything from beginning of string up to and including the occurrence of "= Gauge32: " OR "=STRING: " with "" (maybe the ":[space]" would be better). Problem is I'm not too adept at regex and I get only something akin to "/[^.*:\s]/" which I know is horribly malformed. $output = shell_exec("./script.sh $propertyIP"); $pattern = 'the expression I can't seem to write'; $replace = ''; echo "<pre>" . preg_replace($pattern,$replace,$output) . "</pre>"; clues? Quote Link to comment https://forums.phpfreaks.com/topic/160666-solved-preg_replace-and-regex/ Share on other sites More sharing options...
Ken2k7 Posted June 2, 2009 Share Posted June 2, 2009 preg_replace('#=( Gauge32|STRING): #', '', $str); ? Quote Link to comment https://forums.phpfreaks.com/topic/160666-solved-preg_replace-and-regex/#findComment-847925 Share on other sites More sharing options...
aolong Posted June 3, 2009 Author Share Posted June 3, 2009 preg_replace('#=( Gauge32|STRING): #', '', $str); ? This only replaces "Gauge32: " or "STRING: " with ""; I need to include all characters from beginning of line - "a-zA-z",", "1-9", ".", "-", and "::" - example string to replace: "COLUBRIS-USAGE-INFORMATION-MIB::coUsInfoLoadAverage5Min.0 Gauge32: " Quote Link to comment https://forums.phpfreaks.com/topic/160666-solved-preg_replace-and-regex/#findComment-848461 Share on other sites More sharing options...
aolong Posted June 3, 2009 Author Share Posted June 3, 2009 Correction. Here are more accurate examples of the strings to be replaced: "SNMPv2-MIB::sysDescr.0 = STRING: " "COLUBRIS-USAGE-INFORMATION-MIB::coUsInfoUpTime.0 = Timeticks: " "COLUBRIS-USAGE-INFORMATION-MIB::coUsInfoLoadAverage1Min.0 = Gauge32: " perhaps matching everything up to ":[space]"? Quote Link to comment https://forums.phpfreaks.com/topic/160666-solved-preg_replace-and-regex/#findComment-848478 Share on other sites More sharing options...
MadTechie Posted June 3, 2009 Share Posted June 3, 2009 So this $result = preg_replace('/\s=\s.*?:\s$/m', '', $str); SNMPv2-MIB::sysDescr.0 = STRING: COLUBRIS-USAGE-INFORMATION-MIB::coUsInfoUpTime.0 = Timeticks: COLUBRIS-USAGE-INFORMATION-MIB::coUsInfoLoadAverage1Min.0 = Gauge32: to SNMPv2-MIB::sysDescr.0 COLUBRIS-USAGE-INFORMATION-MIB::coUsInfoUpTime.0 COLUBRIS-USAGE-INFORMATION-MIB::coUsInfoLoadAverage1Min.0 Quote Link to comment https://forums.phpfreaks.com/topic/160666-solved-preg_replace-and-regex/#findComment-848482 Share on other sites More sharing options...
aolong Posted June 3, 2009 Author Share Posted June 3, 2009 I got it... this works: preg_replace('/(.*):\s/', '', $output); now if I could just find the "solved" button... Quote Link to comment https://forums.phpfreaks.com/topic/160666-solved-preg_replace-and-regex/#findComment-848510 Share on other sites More sharing options...
MadTechie Posted June 3, 2009 Share Posted June 3, 2009 But that will wipe out the whole line, i thought you just wanted the = STRING: removed in anycase solved ? (bottom right topic solved) Quote Link to comment https://forums.phpfreaks.com/topic/160666-solved-preg_replace-and-regex/#findComment-848512 Share on other sites More sharing options...
aolong Posted June 3, 2009 Author Share Posted June 3, 2009 Probably my fault, perhaps I was not clear that there are some characters in $output after ":[space]". It's those characters I want preserved, without the preceeding... Thank you for your help. Quote Link to comment https://forums.phpfreaks.com/topic/160666-solved-preg_replace-and-regex/#findComment-848515 Share on other sites More sharing options...
nrg_alpha Posted June 3, 2009 Share Posted June 3, 2009 .* is usually not a good idea. You can read this thread to understand why (post#11 and 14 - it deals with .+ but the principal is the same). Granted, if each string containing ": " is on their own lines, it might not be an issue.. so it is circumstantial.. Just note that the capture (the set of parenthesis) surrounding .* isn't necessary.. the pattern could simply be: /.*:\s/ Granted, you mentioned from the beginning of the string, so you should include ^ at the beginning, and if there are multiple lines, you could add the m modifier after the closing delimiter: /^.*:\s/m In the event you find out you don't want to replace just anything under the sun that contains :[space], you could perhaps use something like: Example: $str = 'SNMPv2-MIB::sysDescr.0 = STRING: COLUBRIS-USAGE-INFORMATION-MIB::coUsInfoUpTime.0 = Timeticks: COLUBRIS-USAGE-INFORMATION-MIB::coUsInfoLoadAverage1Min.0 = Gauge32: '; $str = preg_replace('#^[^=]+=\s(?:Gauge32|STRING):\s#m', '', $str); echo $str; Output: COLUBRIS-USAGE-INFORMATION-MIB::coUsInfoUpTime.0 = Timeticks: You can simply append any additional to the conditional (?: ... | .... ) to extend this list of targets to wipe out. Quote Link to comment https://forums.phpfreaks.com/topic/160666-solved-preg_replace-and-regex/#findComment-848539 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.