Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Removing empty lines

New Here ,
Mar 15, 2007 Mar 15, 2007
I have a larger string that contains many lines. Some of these lines happen to just be tab characters or just an empty line. I am trying to remove all lines that do not contain any letters. An example would be this:

quote:


SPL 2 INVOICE 2121 -19.54 1 Less: Average Employee Cost: $ 224.00 19.54 ? Tax Calculation









SPL 3 INVOICE 5350 1 Taxable Service Fee: 325.60 ? Detail



I am trying to turn something like that into this:

quote:


SPL 2 INVOICE 2121 -19.54 1 Less: Average Employee Cost: $ 224.00 19.54 ? Tax Calculation
SPL 3 INVOICE 5350 1 Taxable Service Fee: 325.60 ? Detail



Is there some sort of function that could do this?
1.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 15, 2007 Mar 15, 2007
Is there some sort of function that could do this?

Or a group of functions. My first instinct would be to look at the
regex functions if you are confortable with that syntax. You would use
the reFind() and reReplace() functions to use regex in ColdFusion.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 15, 2007 Mar 15, 2007
You can do a search and replace where any instance of more than one consecutive "space" gets replaced with a single "space".

I would start by looking at "ReReplace" and string tools to do this.

Trim() is what came to my mind, but that will only remove the leading and trailing spaces. If your original string is put together as a result of data entry and resulting appending of new data entry, you might consider putting the Trim() function around your data before it gets inserted in the database.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 15, 2007 Mar 15, 2007
Thanks for the quick replies. Regex is probably going to be what I need but I am not very skilled at it.

I tried this:

quote:


<cfset works = REReplace(#works#, "^[a-z0-9A-Z]", "", "ALL")>



but that does not work. the problem with the "You can do a search and replace where any instance of more than one consecutive 'space' gets replaced with a single 'space'." idea is that some of the lines have multiple empty spaces in them but I need that because this is a tab-seperated file so I need to preserve the empty tabs. What I do need to remove is entire lines that do not have any letters or numbers in them.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 15, 2007 Mar 15, 2007
I'm not the strongest RegEx person either. But in general you should be
able to look for lines that are empty and replace them. I would suspect
you would be using the start and end line token as well as what you
provided. You may also what to check out the character meta tags like
[[:space:]] which I think is one.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 15, 2007 Mar 15, 2007
I am not nearly as good at regex as Ian, so I rely on simple replaces. For this one, the first thing I'd try is to replace a double linefeed/carraige return with a single one.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 15, 2007 Mar 15, 2007
Here's a regex you can use: [\r\n]\s*([\r\n]|\Z)

[\r\n] - matches any carriage return or line-feed/new-line character (this can mark the beginning or end of a line)
\s* - matches 0 or more whitespace characters
([\r\n]|\Z) - matches any carriage return or line-feed OR the end of input (EOF)

You will want to replace any matches with a single CRLF, which in ColdFusion is "#chr(13)##chr(10)#"

So just do: reReplace( myString, "[\r\n]\s*([\r\n]|\Z)", "#chr(13)##chr(10)#", "all" )

Then you may want to follow that up by passing the new string value to the trim() function, because the replace may tack an extra line break on to the end of the string. I did not throughly test this regex but it appears to do the job.

There is great plugin for Eclipse called QuickRex that can do real-time evaluation of regular expressions in multiple formats and also provides content assistance (ColdFusion uses Perl format to the best of my knowledge.) A tool like this makes building regex much easier and faster IMO.

Hope this helps,

Justin
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 15, 2007 Mar 15, 2007
LATEST
Thank you so much that did the trick perfectly! I was banging my head trying to get it to work. In the meantime I found a really good regex tool over here: http://www.cuneytyilmaz.com/prog/jrx/

thank you very much for the help.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources