0

I have a query which loads a lot of fields, and removes duplicate spaces, tabs and line breaks.

regexp_replace(FIELD, E'[\|\\s\\n\\r]+', ' ', 'g' ) as FIELD

When I test this using Sql Manager for PostgreSQL (Windows Environment), it works as expected. But...

But this query is at a PHP file, which is ran daily using crontab (Linux Environment), it remove duplicate spaces, tabs, line breaks and "s"

Example, the string ahead:

"Small 

unicorns are   smart"

Turns into:

"mall unicorn are mart"


Why is that happening?

2
  • 2
    You probably need to double escape those \'s, or remove the double escaping, without the actual code it is hard to tell. What do you see if you just echo your query? Commented Jan 29, 2014 at 21:01
  • And also \r and \n are subgroup of \s (whitespace). Commented Jan 29, 2014 at 21:10

1 Answer 1

1

You don't want to double escape the \ in php (unlike, say, Java). You're actually escaping the backslash, turning the special characters into literal letters. Or, if I'm totally wrong, you actually need a double escape.

Try:

[\|\s\n\r]+

Also, as Maxim points out, \r and \n are already included in \s.

Edit:

looks like you're trying to include a literal pipe in there too. In that case, use [|\s]+ http://regex101.com/r/nE3dI8

If you need it double escaped, [|\\\s]+

Sign up to request clarification or add additional context in comments.

7 Comments

But "\" has a meaning inside a E'' string in PostgreSQL so you need to double them to get a single "\" down to the PostgreSQL regex engine. Then you probably have to escape each of those to get them past PHP.
@muistooshort blast, you're probably right. So probably needs to be double escaped [|\\\s]+
Best bet is to experiment on sqlfiddle.com to get the right number of escaped escapes and then double them to get them through PHP. What a mess.
@muistooshort I think perhaps \\\\\\\\\\\\\\\\\\\\\ :P
@muistooshort can I wrap it in Haskell generated by COBOL too?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.