1

I have two data testing12334 which can be written as

testing[0-9]*$ 

in regular expression then I have testing33ab_1abckd which I can write as

testing[0-9][a-z][_][0-9][a-z].

I am trying to make one reg exp that works for both. Struggling any insight?

UPDATE: in shell(.ksh)

1
  • In your second example, your regex does not match the text. It's not clear what you're trying to match. My guess is that you mean testing[0-9]+[a-z]+_[0-9]+[a-z]+, which is not what the answers currently given are going to match. Then again, you might mean testing[0-9a-z]+_[0-9a-z]+ or maybe just testing[0-9a-z_]+. You should clarify. Commented Sep 17, 2014 at 19:10

2 Answers 2

1

You can use:

\btesting\w+\b

RegEx Demo

Or in shell you can use equivalent:

grep -E '\btesting[[:alnum:]_]+\b'
Sign up to request clarification or add additional context in comments.

8 Comments

You didn't mention ksh in question but yes it will work with grep -E
That doesn't seem equivalent at all. It will match testingX for example
echo "$a" | grep "^testing\w+\b*$" awk: warning: escape sequence \w' treated as plain w' getting error..
You can use: grep -E '\btesting[[:alnum:]_]+\b'
isn't their easier way? [A-Z0-9]([0-9][A-Z][_][0-9][A-Z]) by this [A-Z0-9] I mean, start with either num or character
|
0

Make the suffix part optional

testing[0-9]*([a-z][_][0-9][a-z]+)?$

In basic regular expressions (e.g. traditional grep and sed), you need to backslash the metacharacters. For extended regular expression syntax (basically grep -E and most everything newer than that, save for Emacs) this should work as-is.

3 Comments

I want if either could start with 0-9 or a-z
I just copy/pasted your allegedly working regex. I don't understand your additional requirements. What's testing1**? What's "either"?
I added the missing wildcard after testing[0-9] but the rest will have to be specified better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.