tags:

views:

103

answers:

2

I need a regular expression which extract type specifier ( like %d,%s) from a string.

   <?
     $price = 999.88;
     $str = "string";
     $string = "this is a %f sample %'-20s,<br> this string is mixed with type    specifier like   (number:%d's)";        
    //output 1 :echo sprintf($string,$price,$str,500);
    //output 2 should be $string replaced by [#]
   ?>

output 1

this is a 999.880000 sample --------------string,
this string is mixed with type specifier like (number:500's)

i want to replace all these type specifiers with [#]. how do i write an regular expression for that type specifiers.

what i need is

output 2

this is a [#] sample --------------[#],
this string is mixed with type specifier like (number:[#]'s)
+1  A: 

Hi,

Just is not very-well tested, but what about something like this :

$format = "this is a %f sample %'-20s,<br> 
this string is mixed with type specifier like (number:%d's)";

$result = preg_replace("#(%[+-]?(([ 0]?)|('.)))-?(\d*)(\.\d*)?[%bcdeufFosxX]#", '[#]', $format);

var_dump($result);

And the result you get is :

string 'this is a [#] sample [#],<br> 

this string is mixed with type specifier like (number:[#]'s)' (length=92)

Which kinda looks like the string you were asking for.


According to the documentation of sprintf, the format has to be :

  • % sign
  • optional sign specifier : + or -
  • optional padding specifier : ' ' or '0'
    • An alternate padding character can be specified by prefixing it with a single quote (')
  • optional alignment specifier : nothing or '-'
  • optional number, a width specifier
  • optional precision specifier : period (`.') followed by an optional decimal digit string
  • type specifier : one character amongst the ones allowed

This what I tried to match with that regex ^^


Hope this helps ! Have fun ^^

Pascal MARTIN
yes man its working...Thank you :)
coderex
You're welcome :-)(BTW, There's definitly gotta be a way to make this easier to read/understand/maintain ; like splitting the regex in several lines, one for each number on sprintf's documentation page... This can probably be done with a modifier for the regex ; mabye 'x' ; see http://fr3.php.net/manual/en/reference.pcre.pattern.modifiers.php for more informations ;-) )
Pascal MARTIN
one more doubt, %1$d is this format works??
coderex
Oh, no, it doesn't :-( (didn't know that this was even possible ! thanks for the information, btw :-) ) ; now, I suppose you have to insert something in the pattern somewhere (I can't find a "real" description saying where it should be located ; just before the last character ? )
Pascal MARTIN
@pascal: i think Tom Haigh did that in his code. please check that. :)where you guys learned these informations ?? ;)
coderex
@pascal got any resources??? :(
coderex
Where we learnt that ? About regexes, I did a bit of Perl before PHP ; and Perl is really regex-oriented ^^ About sprintf, well, I didn't know everything ^^ And yes, Tom's regex seems to deal with the %$2d thing, AFAI can tell :-)
Pascal MARTIN
+2  A: 

Pascal beat me to it, but here is my (equally not very well tested regex).

'/%(?:(?<swap_position>[0-9]+)\$)?(?<sign>-|\+)?(?<padding>\'.|0|[[:space:]])?(?<alignment>-?)(?<width>[0-9]+)?(?:\.(?<precision>[0-9]+))?(?<type>[%bcdeufFosxX])?/m';

I would be concerned that by converting everything to # you lose the information where someone has swapped the order using 2$, 1$ etc.

I wanted to make it so that if you captured a custom padding character prefixed by a quote, that quote wouldn't be captured, but I couldn't work it out.

Tom Haigh
@Tom-haigh: let me check with your expression, i will let you know..
coderex
yes it is working, is there any exceptions on your expression?;) because am not aware of this expression making. where you guys learned these informations please tell me about some good resources.
coderex
You'll find the exceptions when you test it. I've tested it a bit, but not extensively so I make no guarantees..
Tom Haigh
thank you very much, any way if there any error i will info you hehe ehe :) (just kidding) . please give me some resources of the regular expression study. :)
coderex
make sure you understand the paragraph in my answer about the order swapping. otherwise this will cause you serious problems.
Tom Haigh
one more doubt ---------regu[]lar expre[# # 1]ssio [# # 2]nssadas das dasd as das dasdssd [# # 3]dfs dfsdf sd fsdfds------------ how to extract [# # 1] [# # 2] [# # 3] ...etc what is the expression
coderex
@mittu: ask this as another question
Tom Haigh