I want to have a regular expression that identifies the items in a
line from a comma-separate values (CSV) file.
Imagine one style of CSV, in which such items are all quoted (Format 1):
"First Item","String","0","Yes","Yes","No","The contents of the
string in the first item"
"Authority","ID","0","Yes","No","No","ID of the person
""responsible"" for the item, if known"
In CSV, double-quotes permit embedding commas (and spaces?) in record
fields. Double-quotes in such fields are escaped by doubling the
character.
The regex that matches the full text of the item is fairly
straightforward:
"((""|[^"]*)*)" # In quotes, a run of double-quotes and anything
else not a quote; make $1 hold the unquoted string
However, a field may be empty (represented by no characters between
the commas). This a special case of the less-paranoid (and arguably
more standard) way of writing the file (Format 2):
"First Item",String,0,,Yes,No,"The contents of the string in the
first item"
Authority,ID,0,Yes,No,No,"ID of the person ""responsible"" for the
item, if known"
The something-between-quotes regex doesn't pick up the nonquoted
fields (obviously).
So make the regex fancier, to make the quotes optional and recognize
the field separator (which does not exist at the end of the record):
("?((""|[^"]*)*)"?),?
This still works for Format 1, but in Format 2 it matches the whole
of any run of records that aren't quoted (String,0,Yes,Yes,No,").
Start from the other end, and try a regex that matches fields not
quoted:
([^,[:cntrl:]]*),? # any run of characters, including blanks, that
aren't controls or commas, and may end in comma
The exclusion of control characters prevents the matching of:
"The contents of the string in the first item"
Authority
If the next field is a quoted string with a comma in the middle, this
pattern stops at the embedded comma.
So maybe a pattern that combines the two patterns would work:
(("?((""|[^"]*)*)"?)|([^,[:cntrl:]]*)),? # match quoted fields if
you can, unquoted fields if you must.
No: This pattern matches
String,0,,Yes,No,"
in the first line of the Format 2 example. It's the same behavior as
the quoted-only pattern (matches runs of nonquoted strings).
Reversing it:
(([^,[:cntrl:]]*)|("?((""|[^"]*)*)"?)),?
behaves the same as the nonquoted pattern (matching stops at commas
within quoted strings).
I'm out of ideas. Does anybody have a suggestion?
— F
Is there a way, to see an RTF document without the RTF markup in it?
I have some 100 RTF files, and I need to do a global search and
replace. It is no problem I think, to do this in Textmate, cause the
things I will replace wont appear in any RTF commands, but i would
like to view the files properly at least. looking at all the RTF
markup is just a little confusing :)
Any suggestions on how to this another way? I am up for suggestions.
Thx in advance,
Thomas Krajacic
My quest for supporting CSV whizbangs in TextMate continues. I
created a very simple CSV language bundle, and can't get my CSV
commands (OK, so far, "command") to apply to the file.
I added a new bundle and named it CSV. Here is the language spec:
=============
{ scopeName = 'text.csv';
fileTypes = ( 'csv' );
}
=============
Simple enough. My problem may be that it's too simple. I'm figuring
that patterns and folding don't make much sense in a language as
simple (hah!) as CSV.
Opening a .csv file identifies the file as CSV (the language popup
says "CSV"). However:
- Trying to detect the scope inside the file (⌃⇧P) produces no
scope tip at all.
- The commands popup at the bottom of the window starts you off at
the top of the language list, not at CSV.
- Pressing ⌘R (the key equivalent for one of my CSV commands) gives
me a popup offering the ⌘R commands of two other bundles (including
Xcode)... which I now see specify no scope for those commands. My ⌘R
is scoped for text.csv.
Clearly my language spec does not suffice to give the contents of
a .csv file text.csv scope. I'm missing something. What is it?
— F
While we're on the subject of the blogging bundle (i.e., after reading
the previous post which reminded me), what's the status of Movable Type
categories? Any hope of getting them supported? At present, that's one
of the only things keeping me from using TM for all my blogging
(instead, I use MarsEdit along with a plugin that lets me convert
keywords to tags).
Yours,
Andrew
I'm not sure how to put this without sounding like a demanding
ingrate, but here goes...
I've been using Brad Choate's persistent include commands since the
day they came out, because they are brilliant for my way of working.
But (here it comes) they would be so much more brilliant still if it
was possible to update any files selected in the project drawer
whenever an included file was changed.
I know about TM_SELECTED_FILES, but I can't see how to apply a
command to all of them in place. The only thing I could think of
involved creating temporary files, which doesn't seem the right way
to be going about this.
Any suggestions?
A new file in TextMate:
====================
p "testing rubymate"
====================
command+R to see the output in RubyMate and here's what I get:
Output in Run Window:
/bin/bash: line 4: DISPLAY has been set to :0.0 /usr/bin/ruby: No such
file or directory
Where did my precious RubyMate go?
This seems to happen quite often...
I have:
<h2>Download Our Catalog</h2>
Which needs to become:
<h3>Download Our Catalog</h3>
How would I go about writing a snippet/command that would allow me to
select "<h2>Download Our Catalog</h2>", activate the snippet, and have
the tag become selected ("h2" in this case) and mirrored in the
closing tag, therefore allowing me to simply type "h3" and then tab
back out?
the indentation looks as if it is indenting the next line if there is
not a semicolon on the end of the previous line which is fine except
when the previous line is a comment like this
#init var
$var = 2;
should be
#init var
$var = 2;
i notice that this is only happening since allan's updates.
Thanks
Kim
I often find that I paste something into a document, then immediately
select it to do something to it, like wrapping it in tags or
converting characters to entities. Long ago I used an editor
(possibly alpha) that had a 'select pasted text' command, that did
the selection straight after a paste, without having to drag over it
or whatever. I can't find an equivalent command in TextMate, nor any
environment variables pointing to the start and end of the latest
pasting that would let me implement it myself.
Have I missed something? Either a command in some bundle I don't know
about or some other way of achieving the same effect?
If not, here's a feature request. Any one of the following:
Select Pasted Text command
Paste and Select command (extra modifier key on cmd-V)
Select after Pasting preference.
Thanks for any help or pointers.