pyparsing: Matching comment lines - pyparsing

I'm building a partial parser for the NATURAL programming language. A comment starts with "/*", "* " or "** " and ends together with the line. The latter two patterns are used to mark an entire line as comment, so they may be preceded only by whitespace. For the first type,
parser.ignore("/* " + SkipTo(lineEnd))
works fine. For the second type, I tried
parser.ignore(lineStart + Optional(White(" ")) + "* " + SkipTo(lineEnd))
which works when "* " is at the beginning of the line, but not when it is preceded by spaces.
What am I missing?

Related

How to remove double quotes(") and new lines in between ," and ", in a unix file

I am getting a comma delimited file with double quotes to string and date fields. we are getting " and new line feeds in string columns like below.
"1234","asdf","with"doublequotes","new line
feed","withmultiple""doublequotes"
want output like
"1234","asdf","withdoublequotes","new linefeed","withmultipledoublequotes"
I have tried
sed 's/\([^",]\)"\([^",]\)/\1\2/g;s/\([^",]\)""/\1"/g;s/""\([^",]\)/"\1/g' < infile > outfile
its removing double quotes in string and removing last double quote like below
"1234","asdf","withdoublequotes","new line
feed","withmultiple"doublequotes
is there a way to remove " and new line feed comes in between ", and ,"
Your substitutions for two consecutive quotes didn't work because they are placed after the substitution for a sole quote, when only one of the two is left.
We could remove " by repeated substitutions (otherwise a quote inserted by the substitution would stay) and new line feed by joining the next input line if the current one's end is no quote:
sed ':1;/[^"]$/{;N;s/\n//;b1;};:0;s/\([^,]\)"\([^,]\)/\1\2/g;t0' <infile >outfile

Dynamic Query with MATCHES in Progress 4GL

I have to create a query in which people can input some values or not. If they don't input them, the program will search for everything. And people can put just a part of the data of the code. My code became something like this.
cQuery = "" +
" FOR EACH table1 " +
" WHERE table1.status_ = 1 " +
" AND table1.section MATCHES " + QUOTER("*"+ pc-section + "*").
So my question.
1 - I read a lot that MATCHES uses a table scan and we should avoid it so I thought about putting MATCHES in an if variable. Is it really necessary?
2 - is the QUOTER(""+ pc-section + "") the correct way of using MATCHES in a dynamic query? My results seem wrong and I don't know if it was because of the MATCHES or if there is another problem in my code.
Sorry for any wrong ideas and thanks for your time.
EDIT:
My IF statement is like this:
DEF VAR ifSection AS CHAR.
IF pc-section <> "" THEN ifSection = " AND table1.section MATCHES " + QUOTER("*"+ pc-section + "*").
And in my code i use:
" FOR EACH table1 " +
" WHERE table1.status_ = 1 " + ifSection.
I managed to make it work now. The problem in my results was because I forgot to add 'ifSection' after THEN. But I still am not sure if this is the best way to do it. Is there another better way to use MATCHES in a dynamic query? Thanks for the help
Don't use IF statements in a WHERE clause - you will never, ever use an index that way.
You can put the IF around the outside:
cQuery = " FOR EACH table1 WHERE table1.status_ = 1 ".
IF <something> THEN
// no index
cQuery = cQuery + " AND field-name MATCHES " + QUOTER("*"+ pc-section + "*").
ELSE
IF <something-else> THEN
// may be indexed
cQuery = cQuery + " AND field-name BEGINS " + QUOTER(pc-section).
ELSE
IF <something-else> THEN
// may be indexed
cQuery = cQuery + " AND field-name = " + input-value.
etc, which will help you at least try to use indexes where possible.
Replacing MATCHES with indexed searching is more complex. You could look at word indexes and the CONTAINS keyword as a start.

Remove double quotes from text in r

I want to eliminate double quotes from text in R. Is there a better way to do it?
I tried below code but it's still not removing double quotes:
gsub("\"", "", a$answer)
The problem with what you tried is that you want the regular expression (i.e. pattern) to be \", but backslashes are special to R, so you need to write it twice in R so it ends up as a single backslash in the pattern.
For example,
withquotes <- ' this is a double quote: " '
gsub('\\"', "gone!", withquotes)
# [1] " this is a double quote: gone! "
We can also do this without escaping the double quotes
gsub('"', "gone!", withquotes)
#[1] " this is a double quote: gone! "
data
withquotes <- ' this is a double quote: " '

PadLeft with spaces

Have a relatively simple request.
I wish to pad left a string with spaces in an HTML page using VB on asp.net
For me the most obvious way to do it is
Response.Write(qty.PadLeft(5, " ") + " x " + part_number)
but as HTML does not render multiple spaces, this does not work
my workaround is
Response.Write(qty.PadLeft(5, "0") + " x " + part_number)
which pads the number with zero's but looks fairly unappealing on the website.
Any suggestions?
Thanks
Update:
Based on replies so far I have tried
"100".PadLeft(5, " ")
but this outputs &&100
OK, I solved it by doing this
rep = string.concat(Enumerable.Repeat(" ", 5-qty.Length))
Response.Write(rep + qty+ " x " + part_number)
Not the best but works.
There's & nbsp; or setting or the css (with white-space) to see spaces. If you want to align information, you might want to also look at having a fixed font.

Replacing a new line character with streamwriter remove everything after it. (ASP.NET, Json, C#)

I'm having an unexpected problem which I'm hoping one of you can help me with.
I have an ASP.NET Web API with a number of end points, one of which takes user input, received as JSON, and converts it into an order object which is written to a file in .CSV format. The following is a small snippet of the full code as an example.
using (StreamWriter writer = new StreamWriter(file))
{
writer.Write(escape + order.Notes + escape + delim);
writer.Write(escape + order.Reference1 + escape + delim);
writer.Write(escape + order.Reference2 + escape + delim);
writer.WriteLine();
writer.Flush();
}
The problem I am having is that some users are inclined to add line breaks in
certain fields, and this is causing havoc with my order file. In order to remove
these new line characters, I have tried both of the following methods.
writer.Write(escape + product.Notes.Replace("\n", " ") + escape + delim);
writer.Write(escape + product.Notes.Replace(System.Environment.NewLine, " ") + escape + delim);
However, it seems that, rather than just remove the new line character and carry on writing the rest of the fields, when a new line is encountered, nothing else gets written.
Either everything else gets replace with the " " or nothing else is being written at all, but I'm not sure which.
If I remove the .Replace() the whole file is written again but with extra line breaks.
I hope somebody has experienced this one and knows the answer!

Resources