I have the following type definitions in my code:
type tag =
| Head
| Title
| Body
| H1
| P;;
type domtree =
| Empty
| Node of tag * string * domtree list;;
I need to print the tags along with the strings. But I couldn't find any way to convert the tag (the constructor names in the first type definition) into strings and concatenate them with the string part of the domtree. Is there any specific way to do this? Does OCaml provide a way to convert non-inbuilt types into strings? I found a similar question in here but I didn't quite understand it.
There is no such a facility built in OCaml and you will need to write yourself the conversion function tag_to_string : tag -> string.
It is easy to generate the body of this string automatically, for instance, use this sed one-liner:
sed -e 's/\| \(.*\)/| \1 -> "\1"/'
and paste your tag definition in its standard input. It yields
| Head -> "Head"
| Title -> "Title"
| Body -> "Body"
| H1 -> "H1"
| P;; -> "P;;"
and you just have to clean the ;;.
There is a lot of other solutions to define this boilerplate code, I also love to use Emacs macros for this.
Related
I have a table of rows with cells containing multiple strings. Like this:
K1111=V1111;K1=V1;kv13_key4=--xxxxxsomething;id5=true;impid=23123123;location=domain_co_uk
I need to extract a substring that begins with kv13_key4= and ends with anything after but the lengths all vary and the substrings are all separated by a semicolon ; . I tried
REGEXP_EXTRACT(customtargeting,'%in2w_key4%;') As contains_key_Value
but didn't work. I need something like this:
| Original Cell | Extracted |
| key88=1811111;id89=9990string;K1=V1;23234234234tttttttt13_key4=--x;id5=true;impid=23123;url=domain_co_uk | kv13_key4=--x |
| K1111=V1111;K1=V1;kv13_key4=--xsomething;id5=true;impid=23123123;location=domain_co_uk | kv13_key4=--xsomething |
| ;id5=true;T6791=V1111;K1=V1;kv13_key4=--xxxxxsomething123;impid=23123 | kv13_key4=--xxxxxsomething123 |
Consider below
select *, regexp_extract(customtargeting, r'kv13_key4=[^;]+') as Extracted
from your_table
if applied to sample data in your question - output is
Does this regex work:
(?<=kv13_key4=)[^;]+(?=;)
It captures everything between 'kv13_key4=' and the nearest ';'
Your REGEX_EXTRACT would look like:
REGEXP_EXTRACT(customtargeting,r'(?<=kv13_key4=)[^;]+(?=;)')
I was following string manipulation docs from splunk itself
SPL2 example Returns the "body" field with phone numbers redacted.
...| eval body=replace(cast(body, "string"), /[0-9]{3}[-.][0-9]{3}[-.][0-9]{4}/, "<redacted>");
But when I tried to do query
... | eval hostname=replace(cast(hostname, "string"), /cron*/, ""); | ..
I got error
Error in 'eval' command: The expression is malformed. An unexpected character is reached at '/cron*/, "a");'.
I got confused, what did I do wrong?
Update:
String example:
pods-name-cron-3829hr832
pods-name-cron-8923eh32b
My goal was to remove the cron-<random_id>
You're looking at the documentation for Splunk Data Stream Processor (DSP), which is not Splunk Enterprise. DSP is an advanced method for bringing data into Splunk Enterprise (amongst other things). You are most likely doing a search which is within Splunk Enterprise, and the docs for that are at https://docs.splunk.com/Documentation/Splunk
If you are trying to rename a portion of a field and replace it with nothing, you need to use the replace command
... | eval hostname=replace(hostname, "cron*", "") | ..
For example, | makeresults | eval hostname="cronmaster.acme.com" | eval hostname=replace(hostname, "cron", "") will remove cron from cronmaster.acme.com
Post an example of the string and what you want it converted to, and we can confirm if the replace is sufficient, or if a regular expression is required.
You can use the same command, with a different regular expression. The following looks for -cron- followed by any non-whitespace characters, which is represented by \S+.
| makeresults | eval hostname="pods-name-cron-3829hr832" | eval hostname=replace(hostname, "-cron-\S+", "")
Every different version of Splunk might have different functions available for use. Please refer to the documentation according to the Splunk version you are using.
Try this:
... | eval hostname=replace(toString(hostname), "/cron*/", "") | ..
Here are some links you may find helpful if you are using Splunk 7.3.1:
Conversion Functions
Replace function
In the Robot Framework library called String, there are several keywords that allow us to use a regexp to manipulate a string, but these manipulations don't seem to include selecting a substring from a string.
To clarify, what I intend is to have a price, i.e. € 1234,00 from which I would like to select only the 4 primary digits, meaning I am left with 1234 (which I will convert to an int for use in validation calculations). I have a regexp which will allow me to do that, which is as follows:
(\d+)[\.\,]
If I use Remove String Using Regexp with this regexp I will be left with exactly what I tried to remove. If I use Get Lines Matching Regexp, I will get the entire line rather than just the result I wanted, and if I use Get Regexp Matches I will get the right result except it will be in a list, which I will then have to manipulate again so that doesn't seem optimal.
Did I simply miss the keyword that will allow me to do this or am I forced to write my own custom keyword that will let me do this? I am slightly amazed that this functionality doesn't seem to be available, as this is the first use case I would think of when I think of using a regexp with a string...
You can use the Evaluate keyword to run some python code.
For example:
| Using 'Evaluate' to find a pattern in a string
| | ${string}= | set variable | € 1234,00
| | ${result}= | evaluate | re.search(r'\\d+', '''${string}''').group(0) | re
| | should be equal as strings | ${result} | 1234
Starting with robot framework 2.9 there is a keyword named Get regexp matches, which returns a list of all matches.
For example:
| Using 'Get regexp matches' to find a pattern in a string
| | ${string}= | set variable | € 1234,00
| | ${matches}= | get regexp matches | ${string} | \\d+
| | should be equal as strings | ${matches[0]} | 1234
I want to replace a series of pipeline characters with different values. How would I do this with regular expressions?
Example:
This | is | a | sentence
And | this | is | the | second | one
Final result:
This new is new2 a new3 sentence
And new this new2 is new3 the new4 second new5 one
If substitution values differ only in the numbers at the ends, use the command
:let n=[0] | %s/|/\='new'.map(n,'v:val+1')[0]/g
(See my answer to the question "gVim find/replace with counter" for
detailed description of the technique.)
In case of substitution values that differ essentially from each other, change
the command to substitute not a serial number of an occurrence, but an item of
a replacement list with that number as an index.
:let n=[-1] | %s/|/\=['one','two','three'][map(n,'v:val+1')[0]]/g
To perform the substitutions on every line independently of each other, use
the :global command to iterate one of the above commands through the lines
of a buffer.
:g/^/let n=[0] | s/|/\='new'.map(n,'v:val+1')[0]/g
Similarly,
:g/^/let n=[-1] | s/|/\=['one','two','three'][map(n,'v:val+1')[0]]/g
Define a function:
fun CountUp()
let ret = g:i
let g:i = g:i + 1
return ret
endf
Now, use:
:let i = 1 | %s/|/\="new" . CountUp()/g
is it possible to have multiple expression in one state that are similar? I was hoping to group together a few expressions to make life easy for myself. i want to do something similar below but its not working and only recognise the 1st expr and although it does match the expr it doesnt save into the array using yytext. im guessing im doing something wrong so any help would be appreciated.Thanks
<some_state>"Milk;" |
"Honey;" |
"Cinnamon;" |
"Cardamon;" |
"Rum;" |
"Brandy;" |
"Whiskey;" |
"Aquavit;" |
"Kahula;" { printf("Example"); array[i].addition = yytext;BEGIN(amount_state);}
If flex is allowed, you can use start condition scope like the following:
<some_state>{
"Milk;" |
"Honey;" |
... |
"Kahula;" { printf("Example"); ... }
}
If only AT&T lex is allowed, unfortunately this may be invalid...