How to skip the string between patten in regex?

二次信任 提交于 2019-12-25 02:57:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!