expr match problem in shell - regex

In an sh shell script I wrote the following:
opr=+
echo `expr match "$opr" '[+\-x/]'`
but I get this error when ran:
expr: syntax error
What am I doing wrong? I get the same error when I make opr equal to - and / .
Another interesting thing I found is when I wrote this:
opr=a
echo `expr match "$opr" '[+\-x/]'`
it returns this:
1
This means that it matched the string "a" to one of +, -, x, and /. But that makes no sense!

First case: +
+ has a special meaning to expr:
+ TOKEN
interpret TOKEN as a string, even if it is a
keyword like `match' or an operator like `/'
Second case: a
your regexp is a range operation, matching characters from + to x, which includes most alnums. To make the - be matched literally in a charclass, it must be the first or last character; backslashing it doesn't work.

Related

WPF Regex special character '&' incorrect syntax

In my WPF application I want to validate email with this conditions.
The local part can be up to 64 characters in length and consist of any combination of alphabetic characters, digits, or any of the following special characters: ! # $ % & ‘ * + – / = ? ^ _ ` . { | } ~.
My Regex is
[a-zA-Z0-9!#$%'*+–=?^_`.{|}~/&]{1,64}#[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}
But when i use '&' character it shows the following error
Expected the following token: ";".
I have found my solution.
I had to use &
and
My final Regex is
[^.][a-zA-Z0-9!#$%'*+=?^_`.{|}~-/&]{1,64}#[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}

regular expression: extract number in an expression

Suppose I have the following expression:
"1+3x+52-9-45x+x"
my goal is to extract all the constants:
[1,+52,-9]
I have tried using Python:
re.findall("[+-]?\d+","1+3x+52-9-45x+x")
Result is:
['1', '+3', '+52', '-9', '-45']
which are not correct because the coefficents of x are also extracted.
I also tried:
re.findall("[+-]?\d+[+-]?","1+3x+52-9-45x+x")
But still not working.
Try this Regex:
(?:[+-])?\b\d+\b
Demo
OR
(?:[+-])?\d+(?=[\s+-]|$)
Demo
Explanation(for the 1st Regex):
(?:[+-]) Matching Either a + or a -(Add more operators if you want)
? Making + or - optional
\b\d+\b matching 1 or more digits between 2 non-words(so it will not include the coefficients)
Explanation(for the 2nd Regex):
(?:[+-]) Matching Either a + or a -(Add more operators if you want)
? Making + or - optional
\d+(?=[+-]) matching 1 or more digits(greedy) immediately followed by a + or - or a space or If it is the end of line. You can add more operators if you want.

Regular Expressions with multiple dots in Linux bash shell give strange results

I tried to match a substring including a lot of dots, and it failed in Debian Linux shell. I made a simple script to look how dots are processed and found it completely out of rules. I retried it Bash, perl, Ubunta shell it all the same. The script and output are below.
#!/bin/sh
my_regex=u2734523abcABCB.C123.ABC.abc.1..2.34.2
Numbering=123456789_123456789_123456789_123456789
echo "$my_regex"
echo "$Numbering"
echo `expr index "$my_regex" '(ABC)'`
echo `expr index "$my_regex" '(ABC\.)'`
echo `expr index "$my_regex" '(\.\.)'`
echo `expr index "$my_regex" '(.)'`
echo `expr index "$my_regex" '(\.1)'`
Output:
u2734523abcABCB.C123.ABC.abc.1..2.34.2
123456789_123456789_123456789_123456789
12
12
16
16
16
The first regex should match ABC and return number-position of first character. It works.
The second one should find ABC followed by dot, it looks like it ignores dot.
The third one should find two dots but it finds first occurrence of one dot. Ignores again?
The fourth should find first any character, but it still finds the dot on position 16.
The fifth should find a dot followed by 1, it still finds the first occurrence of dot.
It seems like neither \ nor [ ] (I tried it too), nor the dot itself works as in common regular expression.
Why?
expr index has nothing to do with regular expressions.
expr index STRING CHARS outputs the index of the first occurrance of any of the CHARS in STRING. So your first search for '(ABC)' finds the first left parenthesis, A, B, C, or right parenthesis in your string. The first one is the A at position 12.
'(ABC\.)' does the same thing, except it's now also looking for a backslash or period. But the A is still the first match at position 12.
'(\.\.)' looks only for a parenthesis, backslash, or period. The first match is the period at position 16.
Likewise, all your other searches find the period at position 16, because none of the other characters you're listing come before that.
(On a side note, it's silly to capture the output with backticks only to immediately echo it. You'd get the same result by omitting the echo and backticks.)
You are incorrectly using index function of expr. As per man expr:
index STRING CHARS - index in STRING where any CHARS is found, or 0
So 2 things to note here:
index doesn't do any regex matching
index will find position of any of the char is found in string
If you want regex matching then use:
STRING : REGEXP
like this:
my_regex='u2734523abcABCB.C123.ABC.abc.1..2.34.2'
expr u2734523abcABCB.C123.ABC.abc.1..2.34.2 : '.*ABC'
24
expr u2734523abcABCB.C123.ABC.abc.1..2.34.2 : '.*ABC\.'
25
expr u2734523abcABCB.C123.ABC.abc.1..2.34.2 : '.*\.\.'
32
expr u2734523abcABCB.C123.ABC.abc.1..2.34.2 : '.*.'
38
expr u2734523abcABCB.C123.ABC.abc.1..2.34.2 : '.*\.1'
30
The numbers after each expr command is actually the length of the match.
There is no need to use echo here as expr anyway writes output on stdout.
You might want to take a look at BASH built-in =~ operator for regex matching.

RegEx Confusion in linux shell script

Can someone explain what this does in a linux shell.....
port=$((${devpath##*[-.]} - 1))
I have a variable named $devpath, and one possible value is /sys/bus/usb/devices/usb2/2-1.
I'm assuming that ${devpath##*[-.]} performs some sort of regex on $devpath, but it makes no sense to me. Nor does *[-.] which I understand to mean "one of more of any one of the character '-' or any other character except newline"
When running through a script (this is from usb-devices.sh), it seems that the value of port is always the first numeric digit. Something else that confuses me is the '-1' at the end, shouldn't that reduce whatever ${devpath##*[-.]} does by one?
I tried looking up regex in shell expressions but nothing made any sense and no where could I find an explanation for ##.
Given the variable:
r="/sys/bus/usb/devices/usb2/2-123.45"
echo ${r##*-} returns 123.45 and echo ${r##*[-.]} returns 45. Do you see the pattern here?
Let's go a bit further: the expression ${string##substring} strips the longest match of $substring from the front of $string.
So with ${r##*[-.]} we are stripping everything in $r until the last - or . is found.
Then, $(( )) is used for arithmetic expressions. Thus, with $(( $var - 1 )) you are subtracting 1 from the value coming from ${r##*[-.]}.
All together, port=$((${devpath##*[-.]} - 1)) means: store in $port the value of the last number after either - or . at the end of $devpath.
Following the example below, echo $((${r##*[-.]} - 1)) returns 44 (45 - 1).
There is no regex here. ${var##pattern} returns the value of var with any match on pattern removed from the prefix (but this is a glob pattern, not a regex); $((value - 1)) subtracts one from value. So the expression takes the number after the last dash or dot and reduces it by one.
See Shell Parameter Expansion and Arithmetic Expansion in the Bash manual.

Shell script variable assignment with two values (regular expression)

I'm try to set a variable with two values. Here is an example:
letter='[[:alpha:]]'
digit='[[:digit:]]'
integer='$digit'
float='$digit.$digit'
The integer variable must appear one or more times. The variable float should display the first field (before the dot) zero or more times. How can I do this?
Thanks for help!
-- UPDATE --
It's very good to have the support of all of you. Below the solution that has served me:
letter='[[:alpha:]]'
digit='[[:digit:]]'
integer="${digit}+"
float="[0-9]*\\.[0-9]+"
Thank you guys! :D
I haven't looked into bash's expr command (which I assume you are using) to verify which flavor of regex they use, so you may need to do something like [a-zA-Z] instead of [[:alpha:]] and similar substitutions. But assume you have chosen the right value in letter and digit then this should work:
expr match "$string" "(${digit}*.${digit}*)"
or, using your float variable:
float="(${digit}*.${digit}*)"
expr match "$string" "$float"
Remove the parens if you just want to use the return value rather than returning the actual value matched.
Any of the following would be equivalent regexes for the integer:
integer="(${digit}+)"
integer="(${digit}{1,})"
integer="(${digit}${digit}*)"
Do be aware that there are different "flavors" of regex and in different contexts things need to be escaped where in another context they don't need it.
for egrep and grep -E on the bash command line:
float: [0-9]*\\.[0-9]+
integer: [0-9]+
see chart of egrep regxes at http://www.cyberciti.biz/faq/grep-regular-expressions/ for some hints but needs testing for specific situation
for perl and java:
float: [0-9]*?\.[0-9]+?
integer: [0-9]+?
+ matches preceding char or char class >= 1 times
* matches preceding char or char class >= 0 times
. matches any char
\. matches an uninterpreted period
[0-9] matches the class of any digit
? forces reluctant (non-greedy) matching