Jump to content

[SOLVED] preg_replace and regex


aolong

Recommended Posts

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?

Link to comment
Share on other sites

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: "

Link to comment
Share on other sites

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]"?

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

.* 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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.