ukanth
ukanth

Reputation: 2958

Refactor Regex Pattern - Java

I have the following aaaa_bb_cc string to match and written a regex pattern like

\\w{4}+\\_\\w{2}\\_\\w{2} and it works. Is there any simple regex which can do this same ?

Upvotes: 1

Views: 538

Answers (4)

polygenelubricants
polygenelubricants

Reputation: 383756

I sometimes do what I call "meta-regexing" as follows:

    String pattern = "x{4}_x{2}_x{2}".replace("x", "[a-z]");
    System.out.println(pattern); // prints "[a-z]{4}_[a-z]{2}_[a-z]{2}"

Note that this doesn't use \w, which can match an underscore. That is, your original pattern would match "__________".

If x really needs to be replaced with [a-zA-Z0-9], then just do it in the one place (instead of 3 places).

Other examples

Upvotes: 2

YOU
YOU

Reputation: 123841

Looks like your \w does not need to match underscore, so you can use [a-zA-Z0-9] instead

[a-zA-Z0-9]{4}_[a-zA-Z0-9]{2}_[a-zA-Z0-9]{2}

Upvotes: 2

Igor Artamonov
Igor Artamonov

Reputation: 35961

Yes, you can use just \\w{4}_\\w{2}_\\w{2} or maybe \\w{4}(_\\w{2}){2}.

Upvotes: 2

Joey
Joey

Reputation: 354566

You don't need to escape the underscores:

\w{4}+_\w{2}_\w{2}

And you can collapse the last two parts, if you don't capture them anyway:

\w{4}+(?:_\w{2}){2}

Doesn't get shorter, though.

(Note: Re-add the needed backslashes for Java's strings, if you like; I prefer to omit them while talking about regular expressions :))

Upvotes: 3

Related Questions