Closed Thread Icon

Topic awaiting preservation: PREG_REPLACE question re: regex (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12829" title="Pages that link to Topic awaiting preservation: PREG_REPLACE question re: regex (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: PREG_REPLACE question re: regex <span class="small">(Page 1 of 1)</span>\

 
Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 07-22-2003 22:08

Greetings!

I have a string, that is in the format of something like 750.520, or even 750.520D, and sometimes something like 750.520D1A. The problem is I need to grab up to (and including) the first letter after the decimal, and discard the rest (everything to the right). So, if I have something like 750.520D1A, I need to end up with 750.520D. To complicate this even further, the number of characters before the decimal, and after the decimal is not static. It could be 28.255D1A, etc.

help?! :-)

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 07-22-2003 22:35

Well this should grab everything including the first letter (but only the first letter)

/(^[0-9\.]*[a-zA-Z]?)/i

I'm not sure what you are doing so I don't know if you want to replace it or just grab the matching bit but you should be able to go from there.

The parentheses are optional

break down would be
^ <-- from the beginning of the string
[0-9\.] <-- look for numbers and periods (the period is a special char so should be escaped. May not need to be though since it's in square brackets.
* <-- Any number of numbers and periods. technically I shouldn't be allowing it to look for more than one period.
[a-zA-Z] <-- now look for letters [a-Z] should work but my old perl version choked on this when I was testing it for some reason
? <-- look for only zero or one letter.




.:[ Never resist a perfect moment ]:.

[This message has been edited by bitdamaged (edited 07-22-2003).]

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 07-23-2003 02:41

Well, I have a field in a MySQL table that contains this code (it's actually a state law number). But the source I use for fetching the actual law text will only work up through the first letter. If there is anything after that, I get an empty result. So I need to keep the field intact, but build a link with the corrected text (I hope that makes sense).

An example:
750.520E1A

Notice the 'law' value in the URL. The above link will work fine, but
750.520E1A
will yield an empty result.

I'll try your code.


mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 07-24-2003 02:15

Pugzly, you should use preg_match() instead of preg_replace()... Here's PHP code based on bitdamaged's regex:

<?php

$invalid = '28.255D1A';
preg_match("/(^[0-9\.]+[a-zA-Z]?)/", $invalid, $matches);
$valid = $matches[0];
print($valid);

?>


Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 07-24-2003 05:15

I should have replied that a variation of Bit's code works:

echo preg_replace("/(^[0-9\.]*[a-zA-Z]?)([a-zA-Z0-9]*)/i", "$1", $offenses_rows[offense])

« BackwardsOnwards »

Show Forum Drop Down Menu