user8227981
user8227981

Reputation: 21

What could be the Regular Expression for the following

$ cat t1.txt:
ABCD_EFG_HIJK
ABCD_HJIJ_IJKL

What could be the Regex for the above two lines . Even for one of the lines

Or

Scenario is 4characters followedby underscore followed by characters ( any number) followed by underscore followed by characters (any number) again underscore characters .. ends with characters.

4characters_(minimum of 1 characters)_(minimum of1 characters)_(ends with minimum of 1 characters).

Note : It starts with 4 characters.

Upvotes: 1

Views: 73

Answers (3)

Vladislav
Vladislav

Reputation: 1

Another option:

[^_\n]+_[^_]+_[^_\n]+

Match everything except new line \n and _ between underscores

Upvotes: 0

Marc Lambrichs
Marc Lambrichs

Reputation: 2892

After edit, the question is to find a regex that matches a string that starts with 4 chars, followed by minimum of 1 group which consists of '_' followed by minimal 1 character.

[A-Z]{4}(_[A-Z]+)+

explanation:

[A-Z]{4}             # exactly 4 picks from A-Z
(                    # group 1 start
 _[A-Z]+             # "_" followed by 1 or more character out of A-Z
)+                   # group 1 end. Repeat group 1 1 or more times.

You can play with it at regex101

In the above regex I've chosen for capitals as characters, since this is suggested by the question. However, this could be a set of letters e.g., which would change the regex to:

[a-zA-Z]{4}(_[a-zA-Z]+)+

Upvotes: 1

Jakov
Jakov

Reputation: 1009

If you mean by any number of character at least one character, this is the most correct answer: /^[A-Za-z0-9]{4}_([A-Za-z0-9]+_)+[A-Za-z0-9]+$/g.

If you want, you can try this solution at regex website: regexr.com

EDIT: If you want to have only capital letters, than you should remove a-z and 0-9 from square brackets.

Upvotes: 0

Related Questions