Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

Java: RegEx: Splitting a space-, comma-, and semi-colon separated list

// Greedy RegEx quantifier used
// X+ = X, one or more times
// [\\s,;]+ = one or more times of either \s , or ;

    String test_data = "hello world, this is a test, ;again";
    _logger.debug("Source: " + test_data);
    
    for (String tag : test_data.split("[\\s,;]+"))
    {
      _logger.debug("Received tag: [" + tag + "]");
    }

Java: RegEx to Remove HTML Tags

// Ref: https://jalbum.net/forum/thread.jspa?forumID=7&threadID=971&messageID=6907

String noHTMLString = htmlString.replaceAll("\\<.*?\\>", "");

JavaScript: String Tokenization and Substring

// Tokenize a comma-separated string
// then recreate the comma-separated list

  var currentTagTokens = currentTags.split( "," );
  var existingTags = "";

  for ( var i = 0; i < currentTagTokens.length; i++ )
  {
    existingTags = existingTags + currentTagTokens[ i ] + ", ";
  }

  // Remove the trailing ", " from existingTags
  existingTags = existingTags.substring( 0, existingTags.length - 3 ); 

JavaScript String Tokenization

//
// String Tokenization with JavaScript
// From http://www.thescripts.com/forum/thread91795.html
//

function makeArray( strLongString )
{
  return strLongString.split( "," );
}

image manipulation in linux

for file in *.jpg
      do
      cp $file $file.orig
      convert -resize 800x600 $file.orig $file
done

smart plaintext wrapping

From http://blog.evanweaver.com/articles/2006/09/03/smart-plaintext-wrapping:
>>>
Very often you need to break plaintext at a specific width while retaining readability.
#!/opt/local/bin/ruby

class String

  def wrap(width, hanging_indent = 0, magic_lists = false)
    lines = self.split(/\n/)

    lines.collect! do |line|

      if magic_lists 
        line =~ /^([\s\-\d\.\:]*\s)/
      else 
        line =~ /^([\s]*\s)/
      end

      indent = $1.length + hanging_indent rescue hanging_indent

      buffer = ""
      first = true

      while line.length > 0
        first ? (i, first = 0, false) : i = indent              
        pos = width - i

        if line.length > pos and line[0..pos] =~ /^(.+)\s/
          subline = $1
        else 
          subline = line[0..pos]
        end
        buffer += " " * i + subline + "\n"
        line.tail!(subline.length)
      end
      buffer[0..-2]
    end

    lines.join("\n")

  end

  def tail!(pos)
    self[0..pos] = ""
    strip!
  end

end

if __FILE__ == $0
  File.open(ARGV[0]) do |f|
    puts f.read.wrap(ARGV[1].to_i, ARGV[2].to_i, ARGV[3] == "true")
  end
end

<<<
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS