From: "Jan E." Date: 2012-04-04T18:17:53+09:00 Subject: Re: RegEx for a string between two symbols Robert Klemme wrote in post #1054952: >> #### >> dictionary = {} >> File.foreach 'C:/dictionary.txt' do |line| >> next if line.strip.empty? # skip empty lines >> parts = line.split('::').map &:strip # split line at the double colon > > That's unsafe because it will break if there is a '::' in the > definition. It's meant to break, because multiple double colons make the line ambiguous (unless you make certain implications about the words and definitions). If you actually want to allow literal double colons in a safe way, you'd have to implement some kind of escape syntax. But I don't think that's necessary. > if %r{\A\s*(\w+)\s*::\s*+(.*?)\s*\z} =~ line > yield $1, $2 # word, def I find the \w+ pattern much too strict for words in a dictionary, since they may include spaces, hyphens or even special characters like umlauts. > Btw, I wouldn't use symbols for the words because there are potentially > many and they are not GC'ed. If the word list actually gets *that* big, you're better off using a database, anyway. -- Posted via http://www.ruby-forum.com/.