James Raitsev
James Raitsev

Reputation: 96391

How can I translate this regular expression from Perl to Java?

Suppose, we need to match:

Anything or nothing, followed by a dot of which there may be 0 or 1, followed by the word "network", where N may come in lower case or upper case.

This works fine in Perl:

^.*(\.?)[Nn]etwork$

How would you match this in Java? I tried

(.*)\\.?(N|n)etwork$

but "blah.Network" does not match

Upvotes: 2

Views: 79

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336148

What's wrong with

^.*\\.?[Nn]etwork$

as in

boolean foundMatch = subjectString.matches("^.*\\.?[Nn]etwork$");

(The parentheses around the dot are unnecessary anyway).

Upvotes: 1

Related Questions