Forum Moderators: coopster
I need to strip out any 10 digit sequence of capital letters and or numbers from a url.
I can do the first bit...
/[0-9][A-Z]/
but how would I specify that it should be 10 digits long? (ie, that a capital letter or a number x10) is what I need?
I'm stripping ASIN's and ISBNS and they will typically be bang in the middlle of a long url with '/' or other non alpha numeric charactrers soruunding them...
Much thanks
Nick
Just noticed that this is not working quite the way I want. (my fault of course!)
/([0-9A-Z]{10})/
Is matching this
www.somesite.com/blah/SDFLK394SD3920SDSKJJ39
it brings out this:
SDFLK394SD
What I have discovered I really need is for the pattern to match ONLY 10 digit sequecnces if they stand alone from all other characters. So the above url would NOT produce a match.
But this would...
somesite.com/blah?id=3939DJEJE8&pid=39
In an ideal scenario, the above url would produce a match.
I've tried this, to no joy...
/^([0-9A-Z]{10})$/
and this
/^[0-9A-Z]([0-9A-Z]{10})[0-9A-Z]$/
but neiter are correct.
Anyone?
Much thanks!
Nick
with the preceeding and trailing non-alphnumeric charsThat's why there are parenthesis... :)
preg_match("/[^0-9A-Z]([0-9A-Z]{10})$¦[^0-9A-Z]([0-9A-Z]{10})[^0-9A-Z]¦^
([0-9A-Z]{10})$¦^([0-9A-Z]{10})[^0-9A-Z]/",$source,$result);
$result[ 1 ] should hold the matching string...
Eh?
[edited by: jatar_k at 5:59 pm (utc) on Nov. 26, 2003]
[edit reason] broke line to fix sidescroll - code should be all on one line to function [/edit]
If you are sure the pattern will be between two signs that are not numerical or alpha, you could use a negated character class.
In other words: 'find a 10 digit alpha numeric sequence between two characters that are not alpha or numeric.' Negation is denoted with a ^ symbol inside the brackets. (Note: I added spaces because I have trouble seeing...)
/( [ ^0-9] [^A-Z] )? ([0-9] [A-Z] {10} ) ([ ^0-9] [^A-Z])?/
Notice there is a question mark after the first and last sequence: that says 'one must be present, but more are optional. Depending on what you are using to search, you also prob want to use the -i switch to make the search case insensitive.