Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Regex to remove blank lines



  • Please log in to reply
15 replies to this topic
Andrio
  • Members
  • 30 posts
  • Last active: Jun 18 2015 06:11 PM
  • Joined: 05 Jun 2014

Hi Everyone, I'm having some trouble figuring out how to remove blank lines from some variables using RegExReplace

 

I've tried this:

variableName := RegExReplace(variableName, "`r`n`r`n","`r`n")

But that doesn't work.

 

I also tried this:

variableName := RegExReplace(variableName, "`n\s","`n")

This one removes one blank line, but ideally I'd like for it to remove all blank lines between, no matter how many there are.

 

 

Any ideas? Thank you!



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013
✓  Best Answer
; Remove all blank lines from the text in a variable:
Loop
{
    StringReplace, MyString, MyString, `r`n`r`n, `r`n, UseErrorLevel
    if ErrorLevel = 0  ; No more replacements needed.
        break
}

This is the third example from the StringReplace docs.
 
Using RegExReplace:
variableName := RegExReplace(variableName, "\R+\R", "`r`n")



Andrio
  • Members
  • 30 posts
  • Last active: Jun 18 2015 06:11 PM
  • Joined: 05 Jun 2014

You know, I tried the StringReplace example (that's also where I got the regex for my first example), but it didn't work.

 

However, this worked perfectly:

 

variableName := RegExReplace(variableName, "\R+\R""`r`n"

 

​Thank you :D



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

You know, I tried the StringReplace example but it didn't work.

Your lines are probably connected with just `n, instead of `r`n. So if you change it to: StringReplace, MyString, MyString, `n`n, `r`n, UseErrorLevel, it may work.
It is common for lines of text in a variable to be connected with `r`n if the contents of the variable was read from a .txt file. An example  of text that would be connected with just `n is if you created a variable like so:
variableName := "
(
test

test
)"

 
Join can be used to change this, for example:
variableName := "
(Join`r`n
test

test
)"

 

The RegExReplace example probably works because \R matches a newline of any type.



Andrio
  • Members
  • 30 posts
  • Last active: Jun 18 2015 06:11 PM
  • Joined: 05 Jun 2014

I see, thank you for the explanation.

 

By the way, do you know the regex to remove all whitespace from the start of a line? Like a few tabspaces?

 

I tried ^\s however that only matches the first tab. I also tried ^(.*)\S but that just made things go crazy, :( 



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

By the way, do you know the regex to remove all whitespace from the start of a line? Like a few tabspaces?

Try this:
variableName := RegExReplace(variableName, "(^|\R)\K\s+")
 

I tried ^\s however that only matches the first tab.

You would need to use the m (multiline) option for that to work. And some other stuff too: see this related thread.



Andrio
  • Members
  • 30 posts
  • Last active: Jun 18 2015 06:11 PM
  • Joined: 05 Jun 2014

Thanks again for the info. 

 

Try this:
variableName := RegExReplace(variableName, "(^|\R)\K\s+")

 

However, this one only works for the first line, but not the rest. I tried the m option (I did m) in the beginning, if that's correct), but that didn't work either.



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

This one doesn't require the m) option. It seems to work for me. Are you doing something different?
 

TestText := "
(Join`r`n
        Target type=Database Instance1
Target type=Database Instance2
   blah
     Target type=Database Instance3
)"
TestText := RegExReplace(TestText, "(^|\R)\K\s+")
MsgBox, % TestText


Andrio
  • Members
  • 30 posts
  • Last active: Jun 18 2015 06:11 PM
  • Joined: 05 Jun 2014

Oh, I'm not sure what happened, but I copied it over again and it's working now. Sorry for the false alarm and thank you again :)



Alpha Bravo
  • Members
  • 1687 posts
  • Last active: Nov 07 2015 03:06 PM
  • Joined: 01 Sep 2011

Try this:
variableName := RegExReplace(variableName, "(^|\R)\K\s+")
 

You would need to use the m (multiline) option for that to work. And some other stuff too: see this related thread.

 

shouldn't that be

(^|\R)\K\R+

as you may remove needed indentation



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

shouldn't that be

(^|\R)\K\R+

as you may remove needed indentation

There was a second question asked about removing the whitespace from the start of all lines.



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

@Alpha Bravo

 

Yours is an improvement over the one I posted for removing blank lines, because mine will not remove the first line if it is blank. ;)



Alpha Bravo
  • Members
  • 1687 posts
  • Last active: Nov 07 2015 03:06 PM
  • Joined: 01 Sep 2011

There was a second question asked about removing the whitespace from the start of all lines.

 

totally missed that, I stand corrected  ;)



Andrio
  • Members
  • 30 posts
  • Last active: Jun 18 2015 06:11 PM
  • Joined: 05 Jun 2014

Okay, hopefully this is the last time I have to ask a question in this thread, but it's driving me crazy. Thanks guys for all the help thus far :)

 

 

So I'm at a point in my program where a variable contains a big list like this:

 

Afganistán<option value="10148">Afghanistan</option>
Albania<option value="10149">Albania</option>
Alemania<option value="4">Germany</option>
Andorra<option value="10002">Andorra</option>
 

 

As you can see, it's a Spanish country name, followed by a HTML option tag for the corresponding country. This is all contained in a variable called combinedList.

 

At this point, I want to strip out the Spanish country name in the beginning, leaving just the HTML code. So, for example, Alemania<option value="4">Germany</option> would become just <option value="4">Germany</option>

 

I'm trying to do this with this regex: U)`am)^[^<]+

 

I actually got that regex from a thread a made a couple months back. It used to always work, however it doesn't anymore. I end up with an empty variable. The only changes I can think of that I made that would impact it was I changed a StringSplit from delimiting with `r`n to just `r. 

 

Does anyone have any ideas why this isn't working? This is the code in case you're curious:

/*
combinedList contains a list of concatenated Spanish country names followed by 
their English names in HTML option tags, names such as this:

Afganistán<option value="10148">Afghanistan</option>
Albania<option value="10149">Albania</option>
Alemania<option value="4">Germany</option>

*/

combinedList := RegExReplace(combinedList, "U)`am)^[^<]+")


kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

There is an extra parenthesis in the options. And the U option is not needed:

combinedList := "
(Join`r`n
Afganistán<option value=""10148"">Afghanistan</option>
Albania<option value=""10149"">Albania</option>
Alemania<option value=""4"">Germany</option>
)"

combinedList := RegExReplace(combinedList, "`am)^[^<]+")
MsgBox, % combinedList