Regular expression: when every alternate character is a digit

Mathew

I am trying to write a Linux command using egrep that validates lines whose even character is a number.

I am doing it this way:

egrep "^(.[0-9])*$" text.txt 

However, this fails for i) empty lines and ii) cases where the number of characters is odd. It should match 123 since the only odd position is indeed a number, but it doesn't.

Valid patterns:

a2b4c6
000000
123
a1b
a1b2c3q

Invalid patterns:

aaaa
0a0a0a
a1bq

Could anyone please tell me what mistake I am making?

EDIT: The only restriction is that every alternate character needs to be even; the length of the entire line might be even or odd. The command above only allows even length ex: 1234,a1b2c3 etc but even 123,a1b are valid but this doesn't match them. My question is: How to handle this?

terdon

Your regular expression will match empty lines because * means "zero or more" so ^(.[0-9])*$ matches on an empty line. You can fix that by using + ("match 1 or more") instead.

Your next problem is that you don't match if the number of characters on a line is even. To avoid that, you can tell grep to also allow 0 or 1 character at the very end:

grep -E "^(.[0-9])+.?$" 

For example:

$ cat file 
a2b4c6
000000


123
aaaa
0a0a0a

$ grep -E "^(.[0-9])+.?$" file
a2b4c6
000000
123

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Regular Expression to select every digit after a particular word

Remove newline character from a digit in the string using PHP regular expression

Regular expression [:digit:] and [[:digit]] difference

Regular expression no match when followed by character

Regular expression for ten digit number

regular expression match digit and characters

perl regular expression digit on string

regular expression to match a digit present n or more times before character and n or less times after it

.NET regular expression to extract 1-4 digit value from mixed character value

regular expression's $ character

Regular expression for special character $

optional character in Regular Expression

Find and replace regular expression with alternate format

regular expression replace removes first and last character when using $1

Regular expression match between string and last digit

Why is this regular expression only capturing the last digit?

pattern followed with no more digit regular expression python

Regular expression - number without same digit beside

groovy script regular expression to get digit

Find specific digit in a number using regular expression

Email Regular Expression should NOT end with a digit/digits

Regular expression to match until '{' character?

Regular expression is not working with single character

A regular expression to exclude a special character

Regular Expression for Illegal first character

repetition and character classes in regular expression

A regular expression for one only character

Target a bell character with a regular expression

Regular expression with "not character" not matches as expected