Given a regular expression, the ask is to find all matches in a string, str.

Most implementations give longest match only. For example, [a]* in str “aaaaaa”, the regex libraries in C++ or python only provide the longest match, i.e. { “aaaaaa”, “” } at position 0 and 6 respectively.

,

How can one obtain all matches like { “” , “a”, “aa”, “aaa”, “aaaa”, “aaaaa”, “aaaaaa”} with C++ regex libraries.

# Tag: expression

## assumptions – Expression simplification – Mathematica Stack Exchange

Here is why:

```
y=Sqrt(a (a+Sqrt(a^2-b))-b) Sqrt(a (-a+Sqrt(a^2-b))+b) Sqrt(b/(a^2-b))
FullSimplify(y,Assumptions->a>0&&b>0&&a^2-b>0)
(*b*)
Plot({y/.{b->-1},y/.{b->1}},{a,-2,2},
PlotTheme->{"BoldColor","Frame"},
PlotLegends->{1,-1},
PlotStyle->{Thickness(0.03),Directive(Thickness(0.01))})
```

## java – “illegal start of expression” em PessoaDAO

Estou com esse erro no meu `PessoaDAO`

:

```
package com.example.pim8;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
public class PessoaDAO {
private conexao Conexao;
private SQLiteDatabase banco;
public PessoaDAO(Context context) {
Conexao = new conexao(context);
banco = Conexao.getWritableDatabase();
public long inserir (Pessoa **pessoa**){
ContentValues values = new ContentValues();
values.put ("nome", **pessoa**.IdNome());
values.put("cpf", **pessoa**.IdCpf());
values.put("encereco", **pessoa**.IdEndereco());
values.put("DDD", **pessoa**.IdDdd());
values.put("telefone", **pessoa**.IdTelefone());
return banco.insert("pessoa", null, ContentValues);
}
}
}
```

Aparentemente ele não reconhece **pessoa**

Alguém pode me ajudar?

## regex – Regular Expression for parsing a decimal number

I have an input field for entering a decimal number. The field should allow only the numbers 0-9, the comma (,) or the dot (.). The comma/dot should be allowed only once. And there should be only two decimals.

The following regex works partially: **value.replace(/(^0-9,.)/g, ”)**

Basically, it replaces everything but numbers, comma and dot with empty string. But I do not know how enforce only one comma/dot and two decimals.

Do you have any thoughts?

## Why does Solve give me an expression with root in it for this equation?

“12.0.0 for Microsoft Windows (64-bit) (April 6, 2019)”

This can be solved by hand without too much difficulty. But

```
Solve({s == 8/27 ((Sqrt(1 + (3/2 t)^2))^3 - 1), t > 0}, t, Reals)
```

results in

```
{{t -> ConditionalExpression(
Root(-16 s - 27 s^2 + 16 #1^2 + 36 #1^4 + 27 #1^6 &, 2), s > 0)}}
```

## parsing – C# Logical and Math expression parser

I needed a **simple** parser that could do both logical and math operations expressed in a string, as well as being able to use variables stored in json.

None of what I found online seemed to do all of the above, so I came up with my own. Below is the source code for my `ExpressionParser`

as well as my `JExpressionParser`

, in case your variables are stored in a JSON.

Hopefully this post will help someone, as well as allow others to improve my code.

Here is the base parser:

```
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
public class ExpressionParser
{
public virtual IDictionary<string, object> Variables { get; }
protected ExpressionParser() { }
protected virtual DataTable GetComputer()
{
var computer = new DataTable();
if (Variables != null)
{
computer.Columns.AddRange(Variables.Select(v => new DataColumn(v.Key) { DataType = v.Value.GetType() }).ToArray());
computer.Rows.Add(Variables.Values.ToArray());
}
return computer;
}
public ExpressionParser(IDictionary<string, object> variables = null)
{
Variables = variables ?? new Dictionary<string, object>();
}
public object Compute(string expression)
{
StringBuilder sb = new StringBuilder(expression);
foreach (var key in Variables.Keys)
sb.Replace(key, $"Sum({key})");
sb.Replace("==", "=");
using (var computer = GetComputer())
return computer.Compute(sb.ToString(), null);
}
}
```

And here is one that works with json objects:

```
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
public class JExpressionParser : ExpressionParser
{
public JObject JVariables { get; set; }
public override IDictionary<string, object> Variables =>
JVariables.Properties().ToDictionary(p => p.Name, p => p.Value.ToObject(conversions(p.Value.Type)));
static readonly Dictionary<JTokenType, Type> conversions = new Dictionary<JTokenType, Type>()
{
(JTokenType.Integer) = typeof(int),
(JTokenType.Float) = typeof(float),
(JTokenType.Boolean) = typeof(bool),
};
public JExpressionParser(JObject jVariables = null)
{
JVariables = jVariables ?? new JObject();
}
}
```

Usage:

```
var variables = new Dictionary<string, object>() { ("budget") = 1000, ("cost") = 965 };
var parser = new ExpressionParser(variables);
Console.WriteLine("We have $" + parser.Compute("budget - cost") + " of budget left.");
```

## replacement – Substitution of expressions in a symbolic expression

From your question, we have

```
num = 3
X = Table(Symbol("x" <> ToString(i)), {i, 1, num});
Y = Table(Symbol("y" <> ToString(j)), {j, 1, num});
```

and we have two lists of values of the X’s and Y’s,

```
{xvals, yvals} = RandomReal({-10, 10}, {2, num});
```

We can use `Thread`

to create our replacement rules like this

```
rules = Join(Thread(X -> xvals), Thread(Y -> yvals));
```

We can apply the rules to any expressions, e.g.

```
ψlist = Flatten@Outer(ψ, X, Y);
ψlist /. rules
(* {ψ(-0.1739, 4.43855), ψ(-0.1739, 1.32993), ψ(-0.1739, 3.49117),
ψ(4.42524, 4.43855), ψ(4.42524, 1.32993), ψ(4.42524, 3.49117),
ψ(-4.26432,4.43855), ψ(-4.26432, 1.32993), ψ(-4.26432, 3.49117)} *)
```

We can also use `rules = Thread /@ {X -> xvals, Y -> yvals} // Flatten`

, which may be easier to read.

## Find a closed formula (not including $sum$) for the expression $sum_{k=0}^{n-1}binom{2n}{2k+1}$

Find a closed formula (not including $sum$) for the expression

$$sum_{k=0}^{n-1}binom{2n}{2k+1}$$

I started by using the fact that

$$binom{n}{k}=binom{n-1}{k}+binom{n-1}{k-1}$$

to get that

$$sum_{k=0}^{n-1}binom{2n}{2k+1}=sum_{k=0}^{n-1}binom{2n-1}{2k+1}+binom{2n-1}{2k}$$

$$=sum_{k=0}^{n-1}binom{2n-2}{2k+1}+binom{2n-2}{2k}+binom{2n-2}{2k}+binom{2n-2}{2k-1}$$

now letting $m=n-1$

$$sum_{k=0}^{m}binom{2m+2}{2k+1}=sum_{k=0}^{m}binom{2m}{2k+1}+2cdotbinom{2m}{2k}+binom{2m}{2k-1}$$

I’m not exactly sure where to go from here or even if this has been helpful. Any guidance/alternative methods would be greatly appreciated!

## operators – How to apply a function if an expression evaluates to true

I’m trying to define a simple function that will accept a symbol and an expression, set the symbol equal to the expression, and then print out info about the equation. Like so:

```
ClearAll(showValue);
Attributes(showValue) = {HoldAll};
Options(showValue) = {"N" -> False, "S" -> False};
showValue(sym_, value_, OptionsPattern()) :=
Module({},
sym = value;
Print(
HoldForm(sym), " = ",
If(OptionValue("S"), ScientificForm) If(OptionValue("N"),
N) value
)
);
```

as you can see, I want to have some options that i can use to change the format of the output. What I would like is so that if the If evaluates to true, that it will return the function header in such a way as to apply the function to the arguments that are to the if statements’ right. If effect, if the options given are:

```
showValue(k,
UnitConvert(Quantity(1, "BoltzmannConstant"), ("Electronvolts")/(
"Kelvins")), "N" -> True, "S" -> True);
```

then the result will be:

```
k = ScientificForm@N@k = 8.61733*10^(-5)eV/K
```

The purpose of this is so I can avoid an extremely large and burdensome branching tree, once I put in more options. My understanding of Mathematica leads me to believe that something like this is possible because we have operator behavior and functions that can return operators, I just don’t know what the syntax for that is suppose to be.

## regex – Regular expression for csv of numbers less than 1,000

I am going to use javascript to validate a string that needs to be:

only digits and commas

can’t end or start with a comma

each number between the commas has to less than 1,000

no negatives

no periods, just digits and commas

I tried d{1,3}(,{1,3})*

but it doesn’t quite do what I mentioned above