## Regular expression for detecting particular textblocks within a file

I am trying to detect the text-blocks using regular expressions.

I have used the following regular expression for doing this:

``````( )*((0-9.)+).+((0-9a-zA-Z){3}).+((( )(0-9a-fA-F){2}){8})
``````

But as we can see in the image, it is not capturing the blocks properly. The column for blue block has variable number of characters between (0-9a-zA-Z).

Is it possible to capture the block here properly using regex? And if yes, what would be the best way to do it?

Any inputs are highly appreciated.

Thanks!

## How to generate a context-free grammar that defines a regex expression

As the title says, I have been asked to generate a grammar that defines the language of regular expressions. The symbols are: + . * | ? char

I tried and came up with this but it doesn’t work when trying to create a parse tree so it’s wrong but I’m so lost on how to even begin to define the different rules. So any help either on the question specifically, on improving/re-writing what I got, or general step-by-step guidance to figuring out how to write a grammar from just being given symbols is appreciated.

## automata – DFA to Regular Expression

I have a DFA

And I don’t know how to turn it into regular expression. I think the problem lies with every state going back to q0, so I can’t figure out how what to do with the symbols.

Would greatly appreciate if someone could help or at least start me off in the right path?

## plotting – What regular cartesian expression \$y=f(x)\$ has a graph of the “number 7-like” shape?

It seems to be a simple question but I’m stuck. I was trying to transform many fuctions that are partly resembling `7` like $$ax^3+bx^2+cx+d$$, `sawtooth wave`, `Heavyside step`, $$|x|$$, a lot of `trigonometry function family` expressions, `elliptic curve` but failed to do what I need exactly. Finally I have to get an `upside-down trapezoid-like` graph like such:

with a single regular cartesian expression $$y=f(x)$$. All I’ve come up with is an `upside-down triange` with poorly drown discontinuities.

However it was made with an implicit formula `0 == ((UnitStep(-y)*(2 + y*(2/3)))/Abs(x)) - 1` and `ContourPlot` after all which’s not acceptable.

``````ContourPlot({0 == ((UnitStep(-y)*(2 + y*(2/3)))/Abs(x)) - 1}, {x, -5,
5}, {y, -5, 5}, Exclusions -> None, WorkingPrecision -> 16)
``````

I know there is a way to do something similar with transformation of a circle on the complex plane but it’s too difficult for me and I expect to produce the plot with reals.

## finite automata – Constructing a NFA from a regular expression

I have the following regular expression $$R=ab^*(epsilon cup c) cup c^*a$$ and I want to construct the NFA that accepts languages defined by that regular expression.
I started by constructing the NFA that accepts $$R=ab^*(epsilon cup c)$$

Next I tried to continue the concatenation part by adding the following to the NFA as follows:

Is that correct? is there a quick approach to construct NFA from long regular expressions?

## javascript – Cron expression parser

### Problem

Write a command line application or script which parses a cron string and expands each field
to show the times at which it will run.

~\$ your-program “*/15 0 1,15 * 1-5 /usr/bin/find”

``````minute 0 15 30 45
hour 0
day of month 1 15
month 1 2 3 4 5 6 7 8 9 10 11 12
day of week 1 2 3 4 5
command /usr/bin/find
``````

### Code

``````/*
* (minute)(hour)(day of month)(month)(day of week)(command)
* * = means all possible values
* - = range of time units
* , = comma seperated time units
* / = increments where start is left and right is how much to increment by till max hit
*/

const COLUMN_SIZE = 14;

const fields = (
"minute",
"hour",
"dayOfMonth",
"month",
"dayOfWeek",
"command",
);

const labels = {
minute: "minute",
hour: "hour",
dayOfMonth: "day of month",
month: "month",
dayOfWeek: "day of week",
command: "command",
};

const fieldMap = {
minute: {
label: "minute",
range: (0, 59),
},
hour: {
label: "hour",
range: (0, 23),
},
dayOfMonth: {
label: "day of month",
range: (1, 31),
},
month: {
label: "month",
range: (1, 12),
},
dayOfWeek: {
label: "day of week",
range: (1, 7),
},
};

const numberParser = (field, value) => {
if (!isNaN(value)) {
value = Number(value);
const (low, high) = fieldMap(field).range;
if (value < low || value > high) {
throw new RangeError(`Invalid range.`);
}
return "" + value;
}
};

const commaParser = (field, value, separator = " ") => {
if (/^(0-9)+(,(0-9)+)*\$/.test(value)) {
const parts = value.split(",");
return parts.join(separator);
}
};

const rangeParser = (field, value) => {
if (/^(0-9)+-(0-9)+\$/.test(value)) {
const (low, high) = value.split("-");
return expand({
low,
high,
range: fieldMap(field).range,
});
}
};

const starParser = (field, value) => {
if (value === "*") {
const (low, high) = fieldMap(field).range;
return expand({ low, high });
}
};

const stepParser = (field, value) => {
if (value.indexOf("/") > 0) {
const (start, step) = value.split("/");
let (low, high) = fieldMap(field).range;
if (start !== "*") {
low = Number(start);
}
return expand({ low, high, step });
}
};

const parsers = (
numberParser,
stepParser,
commaParser,
rangeParser,
starParser,
);

const expand = ({ low, high, range = (), step = 1 }) => {
low = Number(low);
high = Number(high);
step = Number(step);
if (low > high) return ();
const (min, max) = range;
const result = ();
let current = low;
while (current <= high) {
if ((min != null && current < min) || (max != null && current > max)) {
throw new RangeError(`Invalid range.`);
}
result.push(current);
current += step;
}
return result.join(" ");
};

const formattedOutput = (parsed, padding = COLUMN_SIZE) => {
const output = ();
for (let field of fields) {
}
return output.join("n");
};

const parse = (str) => {
if (typeof str !== "string") {
throw new TypeError("Expected a string");
}
str = str.trim();
const parts = str.split(" ");
if (parts.length !== 6) {
throw new TypeError("Invalid cron format");
}
const command = parts(5);
const result = Object.create(null);
for (let idx = 0; idx < fields.length; idx++) {
const field = fields(idx);
let value = field === "command" ? command : "-";
if (field === "command") {
result(field) = value;
continue;
}
for (let i = 0; i < parsers.length; i++) {
const parser = parsers(i);
const result = parser(field, parts(idx));
if (result != null) {
value = result;
break;
}
}
result(field) = value;
}
return result;
};

module.exports = {
parse,
formattedOutput,
};
``````

### Test

``````const test = require("ava");
const { parse } = require("./lib");

test("throws", (t) => {
let error = t.throws(
() => {
parse();
},
{ instanceOf: TypeError }
);
t.is(error.message, "Expected a string");

error = t.throws(
() => {
parse(1234);
},
{ instanceOf: TypeError }
);
t.is(error.message, "Expected a string");

error = t.throws(
() => {
parse("* * * *");
},
{ instanceOf: TypeError }
);
t.is(error.message, "Invalid cron format");
});

test("parse", (t) => {
const { minute, hour, month, dayOfMonth, dayOfWeek, command } = parse(
"*/15 0 1,15 * 1-5 /usr/bin/find"
);

t.is(month, "1 2 3 4 5 6 7 8 9 10 11 12");
t.is(dayOfMonth, "1 15");
t.is(dayOfWeek, "1 2 3 4 5");
t.is(command, "/usr/bin/find");

t.is(parse("*/15 0 1,15 * 1-5 /usr/bin/find").minute, "0 15 30 45");
t.is(parse("1/30 0 1,15 * 1-5 /usr/bin/find").minute, "1 31");

t.is(parse("1/30 1 1,15 * 1-5 /usr/bin/find").hour, "1");
t.is(parse("1/30 1-3 1,15 * 1-5 /usr/bin/find").hour, "1 2 3");

t.is(parse("1/30 0 1,15,16 * 1-5 /usr/bin/find").dayOfMonth, "1 15 16");
t.is(parse("1/30 0 21 * 1-5 /usr/bin/find").dayOfMonth, "21");

t.is(
parse("1/30 1 1,15 * 1-5 /usr/bin/find").month,
"1 2 3 4 5 6 7 8 9 10 11 12"
);
t.is(parse("1/30 1 1,15 */2 1-5 /usr/bin/find").month, "1 3 5 7 9 11");

t.is(parse("1/30 0 1,15 * 1-7 /usr/bin/find").dayOfWeek, "1 2 3 4 5 6 7");
t.is(parse("1-6 0 1,15 * 1-7 /usr/bin/find").minute, "1 2 3 4 5 6");

t.is(parse("1/30 1 1,15 * 1-5 /usr/bin/find").command, "/usr/bin/find");
});

test("throws on invalid input range", (t) => {
let error = t.throws(
() => {
parse("1-60 0 1,15 * 1-7 /usr/bin/find");
},
{ instanceOf: RangeError }
);
t.is(error.message, "Invalid range.");

error = t.throws(
() => {
parse("1-59 24 1,15 * 1-8 /usr/bin/find");
},
{ instanceOf: RangeError }
);
t.is(error.message, "Invalid range.");

error = t.throws(
() => {
parse("1-59 23 1,15 * 1-8 /usr/bin/find");
},
{ instanceOf: RangeError }
);
t.is(error.message, "Invalid range.");

error = t.throws(
() => {
parse("1-59 -23 1,15 * 1-7 /usr/bin/find");
},
{ instanceOf: RangeError }
);
t.is(error.message, "Invalid range.");
});
``````

### Questions

1. I would like to understand how can I make this code production ready?
2. How can I do to make sure it runs on multiple platform?
3. Are the test cases good enough?

## graph theory – Is there a purely set-theoretic expression of the Euler characteristic?

The Euler characteristic of a graph is V – E + F. Where V is the number of vertices, E is the number of edges, and F is the number of faces.

Given a graph $$X$$, can we write $$X$$ as a set and find its Euler characteristic with a formula which only refers to cardinality and union?

I think we found a way, but is this interesting? We would like to find a way to write the Euler characteristic completely set-theoretically. And, we have found a way, but still working on it. Has this already been done?

Edit: the way to do it is to write the graph $$X$$ as a set of edges and faces. Then, let $$A$$ be the set of pairs of $$X$$, and $$B$$ be the set of triples of $$X$$. Then to compute the Euler Characteristic simply count
$$|cup A| + |B| – |A|.$$
This can generalize.

## How to remove "higher/lower harmonics" from expression

let’s say we are given $$sum_{k=0}^n a_k cos(k omega t)$$. How can we tell Mathematica to neglect all terms in the above expression for which $$k_1>k > k_0$$, where $$k_0$$ and $$k_1$$ are specified?

## equation solving – ND gives expression instead of number

I have a function `sol` defined as a solution to an equation (computed numerically). Specifically

``````sol(p_, a_) := NSolve(g(p, x, a) == 0, x, Reals)((1, 1, 2))
``````

Trying it out

``````sol(.2, .51)
sol(.2, .52)
sol(.2, .53)
``````

gives .51, .52, .53 — on this range it behaves like identity. Now I try

``````ND(sol(.2, aa), aa, .51 )
``````

and instead of giving me 1 as I would expect, it gives me very long expression involving variable x. I am not using the variable `aa` anywhere else in the code. And yes, I have done `Needs("NumericalCalculus`“)`.

How is this possible? It seems obvious to me that it has to return a number! How can it return anything else?

## What kind of regular expression support in Apache configuration?

In the Apache documentation, the following example is given for the `FilesMatch` directive:

``````<FilesMatch ".+.(gif|jpe?g|png)\$">
# ...
</FilesMatch>
``````

What kind of regular expression interpretation is going on here? The `|` means “or”, but why does this pattern match a file extension:
`gif` or `jpeg` or `jpg` or `png`
and not:
`gi` followed by either `f` or `j`, followed by `p`, optionally followed by `e`, followed by either `g` or `p`, followed by `ng`“?

Is it related to regular expressions being “greedy”? Why is `|` greedy, but not `?`?

For example, is this alternative equivalent? If not, why not? Or which is preferred? Does it make any difference?

``````<FilesMatch ".+.(gif)|(jpe?g)|(png)\$">
# ...
</FilesMatch>
``````