How to parse > character in Clojure Instaparse? - clojure

I am trying to parse the > character in Clojure Instaparse. I have tried |> and |\> but the parser doesn't seem to recognize any of these. Does anyone know the correct syntax?

You would just handle them as strings. E.g.:
((insta/parser
"S = '<' tag '>'
tag = #'\\w+'
") "<html>")
; [:S "<" [:tag "html"] ">"]
In instaparse, you can use angle brackets <> to hide parsed elements, suppressing them from the tree output.
((insta/parser
"S = <'<'> tag <'>'>
tag = #'\\w+'
") "<html>")
; [:S [:tag "html"]]

Related

how to fill with spaces an inserted expression to a given length during substitution in vim regex

I want to implement behavior similar to setw() and setfill() in c ++. For example i want to replace expression:
15.8
with:
_______15,8
in text and:
10000
with:
_10000
To implement such behavior, the ability to evaluate the substitute string as an expression is helpful.
If e. g. we put
let s:fill = "_"
func! Setfill(fill)
let s:fill = a:fill
endfunc
let s:width = 8
func! Setw(width)
let s:width = a:width
endfunc
func! Fild()
let m = submatch(0)
return repeat(s:fill, s:width-len(m)) . m
endfunc
in a vimrc file, we can use
:call Setw(6)
:%s/10000/\=Fild()/

Why does the M function List.Transform place double quotes around strings with embedded commas?

I have a native Oracle query running in an Excel workbook, and I'm passing the user-supplied values from a table into the queries WHERE clause.
I wrote a quick function in M that I think adds single quotes to a string that's passed in
(x) =>
let
string_format = "'" & x & "'"
in
string_format
I apply this function to a column and then transform the column to a list but any strings with embedded commas are surrounded by double quotes
text_tbl3 = Table.TransformColumns(text_tbl2,{{"Org_Names", string_format}})
text_ls = Table.ToList(text_tbl3)
It's difficult to see, but TD,AMERITRADE is surrounded by double and single quotes like this : "'TD, AMERITRADE'". I want it to read 'TD,AMERITRADE' , so it has the same formatting as the other cells, but I cannot figure out what causes the additional double quotes.
Quoting text
quick function in M that I think adds single quotes to a string that's passed in
Your function is correct. & is the text concatenation operator.
Because you're using a single expression, you could simplify it by removing the inner let..in expression. (If you don't open the advanced editor you won't see the outer let..in expression).
quote_text = (string as text) => "'" & string & "'"
Note: Your screenshot has extra quotes
Your inputs were:
CHASE
CITI
"TD, AMERITRADE"
Which is why you end up with:
'CHASE'
'CITI'
'"TD, AMERITRADE"'
Your cell probably has quotes on "TD, AMERITRADE" but not on the others.
Getting a comma separated list as a single string
Text.Combine(list, separator=", ") will create a string like a CSV file.
let
list_names = table3[Company],
// equivalent to: list_names = {"CHASE", "CITI", "TD, AMERITRADE"},
without_quotes = Text.Combine(list_names, ", "),
list_quoted = List.Transform(
list_names,
quote_text
),
with_quotes = Text.Combine(list_quoted, ", "),
results = [
list_names = list_names,
list_quoted = list_quoted,
string_without_quotes = without_quotes,
string_with_quotes = with_quotes,
without_equal_to = "string = ""CHASE, CITI, TD, AMERITRADE""",
with_equal_to = "string = ""'CHASE', 'CITI', 'TD, AMERITRADE'"""
]
in
results
How do we use that string in a native query?
My query uses SQL but the method is the same for Oracle.
raw_sql_query is your raw query. It uses the parameter #Company
sql_parameters is a Record type that declares all parameters you are using. Here we use your string with the qoute_text function.
Value.NativeQuery inserts the parameters for you.
let
company = "TD, AMERITRADE",
raw_sql_query = "
select * from Table
where Company = #Company
",
sql_parameters = [
Company = quote_text( company )
],
source = Sql.Database(
"localhost",
"Adventure Works"
),
results = Value.NativeQuery(
source,
raw_sql_query,
sql_parameters
)
in
results
How do we test whether the string function is quoting correctly?
First create a new blank query. We call quote_text() to verify the output.
I used a Record named results so you can label and view every value on a single screen.
manual_quote uses the string concatenation operator to quote strings
quote_string( sample_string ) inserts variables into a text template. Both return the exact same string.
Text.Format becomes cleaner the more complicated your template becomes. This function is simple enough it's not necessary.
Your original function
This is what your function in the advanced editor looks like:
let
quote_text = (x) =>
let
string_format = "'" & x & "'"
in
string_format
in
quote_text
You may remove the inner let
let
quote_text_simple = (string as text) =>
"'" & string & "'"
in
quote_text_simple
How you can use optional arguments and string Templates
let
// a custom function to Surround a string with single quotes.
// A optional second argument lets you specify a different character
quote_string = (input_string as text, optional character as text) =>
let
character = if character = null then "'" else character,
template = "#[quote]#[string]#[quote]",
quoted_string = Text.Format(
template,
[
quote = character,
string = input_string
]
)
in
quoted_string
in
quote_string

Exact Match using VBA Replace

I am trying to remove a string from another string using VBA replace function.
The string from which I am trying to remove looks like below which contains cell address concatenated by ;
"$B$1;$B$21;$B$2;$C$3;$B$20;$B$201"
and the string which I would like to remove is $B$2 by say xxx.
The replace function matches all occurrences of $B$2 in the string and gives me the output as below
$B$1;xxx1;xxx;$C$3;xxx0;xxx01
However I would like to search for $B$2 exactly in the string and expect an output like
$B$1;$B$21;xxx;$C$3;$B$20;$B$201
I one way I could think of doing this is by splitting up the string on ;(separator and looping and looking at each value) but I am looking at more direct solution here. Like using pattern matching techniques or something else.
You can include the following ; in the replace operation, to make sure you only match "complete" references. You just need to take a precaution for also matching the last entry, by adding a dummy semicolon at the end:
s = "$B$1;$B$21;$B$2;$C$3;$B$20;$B$201"
find = "$B$2"
repl = "xxxx"
result = Replace(s & ";", find & ";", repl & ";")
result = Left(result, Len(result)-1) ' Remove the final semicolon
Although this works for your case, in a more general exercise, you would also want to test for the preceding delimiter, and then the last two lines would look like this:
result = Replace(";" & s & ";", ";" & find & ";", ";" & repl & ";")
result = Mid(result, 2, Len(result)-2)
you cold use:
Function myReplace(strng As String, findStr As String, replacementStrng As String)
myReplace = Replace(strng & ";", findStr & ";", replacementStrng & ";")
myReplace = Left(myReplace, Len(myReplace) - 1)
End Function
to be exploited in your "main" sub like follows:
strng = "$B$1;$B$21;$B$2;$C$3;$B$20;$B$201"
MsgBox myReplace(strng, "$B$2", "xxx")

Best way to extra array of strings with regular expressions this single string

I have a data table converted from JSON where one of the columns has rows that look like this that are of character class:
"[{u'className': u'Sticker', u'__type': u'Pointer', u'objectId': u'mYz1ietNEt'}, {u'className': u'Sticker', u'__type': u'Pointer', u'objectId': u'FVn0hE5Zar'}, {u'className': u'Sticker', u'__type': u'Pointer', u'objectId': u'ZxUTYYCunL'}]"
What I really want is a vector of the objectId's, so ideally from the above I would get:
['mYz1ietNEt', 'FVn0hE5Zar', 'ZxUTYYCunL']
What's the best way to get there? Or how do I get there at all? For a single string, test, here's what I've tried:
test1 = strsplit(test, split = "}, ")
test1 = test1[[1]]
That's fine, but I can't seem to find a way to get rid of the left bracket { let alone the other portions of the string that are undesirable.
> test2 = strsplit(test1, "{")
Error in strsplit(test1, "{") :
invalid regular expression '{', reason 'Missing '}''
> test2 = strsplit(test1, "\{")
Error: '\{' is an unrecognized escape in character string starting ""\{"
> test2 = strsplit(test1, u"{")
Error: unexpected string constant in "test2 = strsplit(test1, u"{""
> test2 = strsplit(test1, r"{")
Error: unexpected string constant in "test2 = strsplit(test1, r"{""
Ideally I could find some regex expression that could extract all of the objectId fields in one fell swoop into a vector. Is there something like this?
We can extract the identified strings with a regular expression pattern:
library(stringr)
str_extract_all(test1, "(?<=objectId':\\su')(.*?)(?=')")[[1]]
[1] "mYz1ietNEt" "FVn0hE5Zar" "ZxUTYYCunL"

removing '<' '>' chars from string using regexp in matlab

In my simulink i have a propagate signal which look like this:
<foo_boo>
and at source
foo_boo
i would like to build a regular expression the return from
<foo_boo>
simply foo_boo and from foo_boo i would like to get foo_boo.
In other words, i would like a regular expression that remove '>' and '<' from my string and the string can include [a-zA-Z_0-9] chars.
Pretty easy. Use regexprep to search for symbols that contain < or > in your input string and replace them with nothing. In other words:
out = regexprep(in, '<|>', '');
in would be the string you want to operate on (i.e. <foo_boo>) and out contains the processed string.
Example:
in = '<foo_boo>';
out = regexprep(in, '<|>', '')
out =
foo_boo
Since I think logical indexing is the answer to most things MATLAB (the other being bsxfun), I throw this in:
str = '<foo_boo>';
str( (str=='<') | (str=='>') ) = [];
seems there's no need to use regex:
str = '<foo_boo>'
str([strfind(str,'<'),strfind(str,'>')]) = []