How can I create a regex expression that will match only letters or letters with numbers?
I've tried something like (?:[A-Za-z0-9]+|[A-Za-z]+)
.
The regex should give the following result:
123a --> true
123 --> false
abc --> true
abc4 --> true
How can I create a regex expression that will match only letters or letters with numbers?
I've tried something like (?:[A-Za-z0-9]+|[A-Za-z]+)
.
The regex should give the following result:
123a --> true
123 --> false
abc --> true
abc4 --> true
You can try awk
. If your text file contains the lines:
123a
123
abc
abc4
Use this one liner:
awk '{ if ($0 ~ /[0-9]/ and $0 ~ /[A-Za-z]/) {print $0 " --> true"} else { print $0 " --> false" } }' test.txt
or pretty it up as:
awk '{
if ($0 ~ /[0-9]/ and $0 ~ /[A-Za-z]/) {
print $0 " --> true"
} else {
print $0 " --> false"
}
}' test.txt
Result
123a --> true
123 --> false
abc --> true
abc4 --> true
Explanation