问题
my string is
$str='insert into employees values('
I want to ignore everything between insert into and (. I want to skip employees values how should I skip it in regex?
input string:
insert into employees values( nextval( 'public.scheduled_charges_id_seq' ),'shrenik', 555, NULL)
required output:
insert into employees values( nextval( 'public.scheduled_charges_id_seq' ),'XXX', XXX, NULL);
I tried:
([0-9]|\'.*\')
I want to replace the confidential values in strings Which started as insert into the pattern should be limited to braces from inset into employees ( to where it ends . can u please help me? Thanks in advance
回答1:
How about this:
Regex:
\),\s?'([^']+)',\s?([0-9]+)
Working regex example:
http://regex101.com/r/kR9gY4
PHP:
$str = "insert into employees values( nextval( 'public.scheduled_charges_id_seq' ),'XXX', 555, NULL);";
preg_match("/\),\s?'([^']+)',\s?([0-9]+)/", $str, $matches);
echo $matches[1] . ", " . $matches[2];
Output:
XXX, 555
Or if you're going to replace these values, you could do this:
Regex:
(?<=,)\s?[^,]+,\s?[^,]+(?=,)
Working regex example:
http://regex101.com/r/cJ4pM1
PHP:
$str = "insert into employees values( nextval( 'public.scheduled_charges_id_seq' ),'XXX', 555, NULL);";
echo preg_replace("/(?<=,)\s?[^,]+,\s?[^,]+(?=,)/", "'YYY', 999", $str);
Output:
insert into employees values( nextval( 'public.scheduled_charges_id_seq' ),'YYY', 999, NULL);
来源:https://stackoverflow.com/questions/21680934/how-to-skip-the-string-between-patten-in-regex