Regex (PHP) inserta un espacio después de la combinación de letras Usando el complemento Search Regex Wordpress

Firstly, I hate to be a pain but I'd like to know how to specifically do this in the Search Regex WordPress plugin. It's a popular plugin, but I don't understand how the Replace pattern works for it, it suggests that it accepts regex, but ends up just using literals.

How can I use a replace pattern to insert a space after a certain set of letters. I have the search pattern sorted, just need to know how to replace with a space.

So if we supposed I had a search that looked for every instance where 'testing' was followed by a few letters and I wanted to put a space after each instance.

p.ej

testingstring

should be replaced with...

testing string

preguntado el 09 de marzo de 12 a las 14:03

Sorry to be a pain guys, I'd really like to know how this would work with the popular Search Regex plugin in WordPress. If I could get it working in that I'd be happy, it's too much hassle to dig into the WordPress backend all the time and do things manually. -

4 Respuestas

preg_replace('/(testing)([a-zA-Z])/', '$1 $2', $string);

respondido 09 mar '12, 14:03

Deberías usar \b at the start and a negative lookahead for your match:

preg_replace('#(\btesting)(?!\s)#', '$1 ', $origString);

respondido 09 mar '12, 14:03

preg_replace('/('.preg_quote($sSearch, '/').')(?! )/', '$1 ', $sSubject);

respondido 09 mar '12, 14:03

OK, I know my initial question is probably not very clear, but just in case this helps anyone in future, what I eventually realised is that I needed to split up my Regex search term using brackets e.g.

If I wanted to split up some words, where 'testing' was followed by a bunch of other letters than I would search for...

/([tT]esting)([a-zA-Z]+)/

Then the replace pattern would be...

\1 \2

the \1 would match the search pattern in the first set of brackets, then I have a space, and the \2 would match the search pattern in the second set of brackets. So let's take the following string...

foo testingbar 123

Under the search and replace patterns above, the string would be replaced by...

foo testing bar 123

respondido 10 mar '12, 17:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.