CMS Development

SR0
Contributor | Diamond Partner
Contributor | Diamond Partner

Using regex_replace filter in removing empty lines

SOLVE

I'm currently trying to use regex_replace filter to remove empty lines. 

Below is the script i'm trying to convert to hubl. 

 

var stringWithLineBreaks = `
Lorem ipsum dolor sit amet

- 11.6" HD (1366 x 768) Lorem ipsum

- Consectetur adipiscing elit

- Sed do eiusmod tempor 

- Incididunt ut labore

- Et dolore magna aliqua.

- Consectetur adipiscing elit
`;
var stringWithoutLineBreaks = stringWithLineBreaks.replace(/^\s*[\r\n]/gm,'');//remove empty lines
console.log(stringWithoutLineBreaks);

 

 

here's my current hubl filter

 

<td>{{item.description|regex_replace("[m=true/^[\t\n\f\r ]*[\r\n]/gm]", "")}}</td>

 

0 Upvotes
1 Accepted solution
Ntbrown
Solution
Contributor

Using regex_replace filter in removing empty lines

SOLVE

@Gonzalo Escape "\\" and forgot flags... For multiline it's ?m: I believe.

 

@SR0 

Regexes provided also won't work and there's not a way to definitively tell you which will "work" without clearly stated bounds / expectations. Assuming your text is as copied verbatim - just simple newlines without any "non-printable" characters, isn't from rich text / contains HTML in the "real" source as Gonzalo noted, and other similar cases a very simple one is just replacing consecutive newlines / escape sequences with the edge case of the last new line.

 

You could do something like:

 

regex_replace("(?m:^[\\s\\t\\n\\r])+|\\n$","") - The OR operator here just strips last newline as it's the edge case...

 

Or, simpler

 

regex_replace("(?m:\\n$)","") if it's just simple newlines....

 

Or, normalizing the first... if it has mixed sequences.

 

regex_replace("(?m:[\\s\\t\\r\\n]+$)","")

 

Or, an infinite other number of regex that are, again, entirely dependent on your use case and boundaries. The missing case in all of these is, again, HTML, hidden character sequences / non-printable ones etc. I'm sure even given regexes could be improved I wasn't really going for "optimized" here. I was going for "illustrative", so take them cautiously and I didn't test these either. So, there's also that along with the fact I haven't used regexes in quite some time 😉

 

Also worth noting that if it's just simple newlines... You don't even need a regex to do this if you find them overly complex. Simple string ops suffice.

 

@dennisedson Still patiently waiting for unicode operations to be unblocked that are accepted, but yet magically don't work with the filter 🙄

View solution in original post

8 Replies 8
SR0
Contributor | Diamond Partner
Contributor | Diamond Partner

Using regex_replace filter in removing empty lines

SOLVE

Thanks @Ntbrown, I used the first one and it worked. It's think it removed the empty lines, and empty spaces before, after and inbetweent text, whch is what just I needed. 

 

I used js to remove the lines, but I just wanted to maximize the use of Hubl

Ntbrown
Solution
Contributor

Using regex_replace filter in removing empty lines

SOLVE

@Gonzalo Escape "\\" and forgot flags... For multiline it's ?m: I believe.

 

@SR0 

Regexes provided also won't work and there's not a way to definitively tell you which will "work" without clearly stated bounds / expectations. Assuming your text is as copied verbatim - just simple newlines without any "non-printable" characters, isn't from rich text / contains HTML in the "real" source as Gonzalo noted, and other similar cases a very simple one is just replacing consecutive newlines / escape sequences with the edge case of the last new line.

 

You could do something like:

 

regex_replace("(?m:^[\\s\\t\\n\\r])+|\\n$","") - The OR operator here just strips last newline as it's the edge case...

 

Or, simpler

 

regex_replace("(?m:\\n$)","") if it's just simple newlines....

 

Or, normalizing the first... if it has mixed sequences.

 

regex_replace("(?m:[\\s\\t\\r\\n]+$)","")

 

Or, an infinite other number of regex that are, again, entirely dependent on your use case and boundaries. The missing case in all of these is, again, HTML, hidden character sequences / non-printable ones etc. I'm sure even given regexes could be improved I wasn't really going for "optimized" here. I was going for "illustrative", so take them cautiously and I didn't test these either. So, there's also that along with the fact I haven't used regexes in quite some time 😉

 

Also worth noting that if it's just simple newlines... You don't even need a regex to do this if you find them overly complex. Simple string ops suffice.

 

@dennisedson Still patiently waiting for unicode operations to be unblocked that are accepted, but yet magically don't work with the filter 🙄

jpsanchez
Contributor | Elite Partner
Contributor | Elite Partner

Using regex_replace filter in removing empty lines

SOLVE

Hi, 

 

Not sure if i can help on this 😉 will try my best! 

 

First i will identify txt format is a txt or enrichtxt or HTML?. (the payload) . Second i will try to add  (\l)  to your command line. 

Other work arround. I understand you are looking to wrangle as a "make-up". I suggest you to clean it before, you can do this with the new HubSpot Hub Operations with in a workflow. ( the have some tranformation out-of-the-box new commands) or you can use apps like Trifacta or dataprep to do this. 

 

Hope it helps, not sure about it 😉

Best!

JP

😉

 

 

SR0
Contributor | Diamond Partner
Contributor | Diamond Partner

Using regex_replace filter in removing empty lines

SOLVE

It's in text format. I added \l but it results to syntax error

<td>{{item.description|regex_replace("\l[m=true/^[\t\n\f\r ]*[\r\n]/gm]", "")}}</td>

 

0 Upvotes
Gonzalo
Top Contributor | Diamond Partner
Top Contributor | Diamond Partner

Using regex_replace filter in removing empty lines

SOLVE

I actually know barely nothing about regex, used a few times only Denis xD

 

Although I would point out a few things:

- regex_replace filter uses Java RE2 syntax.

- If the string comes from a rich text, Hubspot usually populates empty lines with <p>&nbsp;</p> so you may need to consider the source of it.


Said that, I have found this regex that may work:

{{ item.description|regex_replace("^[ \t\n]*$","") }}

At least it seems to work in regex101

If this answer helps you to solve your questions please mark it as a solution.

Thank you,


Gonzalo Torreras

HubSpot freelance developer

hola@gonzalotorreras.com
www.gonzalotorreras.com
Schedule a meeting
SR0
Contributor | Diamond Partner
Contributor | Diamond Partner

Using regex_replace filter in removing empty lines

SOLVE

Hi @Gonzalo, I tried the regex syntax you suggested. Unfortunately, that too didn't work. 

 

Thanks

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Using regex_replace filter in removing empty lines

SOLVE

For someone who barely knows anything about it, look at you go 😀

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Using regex_replace filter in removing empty lines

SOLVE

@jpsanchez@Gonzalo  somehow I feel like regex is in both of your wheelhouses 😎

0 Upvotes