Yugesh
Yugesh

Reputation: 4092

How should I write a regex to match a specific word in Java?

I've been trying to get a specific regex working but I can't get it to do what I need.

I want to look for OFF in given string. I want Regex to Match Only string with OFF. I want RegEx pattern to implement it on my android Application.

My Conditions:-

1) Before the first Letter O it will whatever it may be any special character or number etc.. but no Alphabet before O.

2) After the Last Letter F it will whatever it may be any special character or number or dot or ! etc.. but no Alphabet after F.

Tried RegEx Patterns:-

\W*(off)\W*
(\d|\s|%)+off

Java Coding

public static boolean offerMatcher(String message_body) {

    MessageMatcher = Pattern.compile(message, Pattern.CASE_INSENSITIVE).matcher(message_body);

    return MessageMatcher.find();
}

Note:- Patterns are in For Loop.

Example:-

some text you have 20% OFF. Avail @ this shop. - Match

some text some text office address is..... - Not To Match

some text you have 20%OFF. Avail @ this shop. - Match

some text you have 20%OFF! Avail @ this shop. - Match

some text you have 20%OFF; Avail @ this shop. - Match

some text some textofficeaddress is..... - Not To Match

I've been trying to get it right using a Regex generator online but I can't get it to match exactly.

Upvotes: 0

Views: 2570

Answers (3)

JB Nizet
JB Nizet

Reputation: 691645

You just need to drop the * after \W, since it allows for 0 occurrence of a non-word character before and after OFF.

The problem is that it will not find OFF if it's at the beginning or end of the string. If you also want to accept these cases, add them explicitly:

public class Match {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("(\\W|^)(off)(\\W|$)", Pattern.CASE_INSENSITIVE);

        String[] strings = new String[] {
            "some text you have 20% OFF. Avail @ this shop.",
            "some text some text office address is",
            "some text you have 20%OFF. Avail @ this shop.",
            "some text you have 20%OFF! Avail @ this shop.",
            "some text you have 20%OFF; Avail @ this shop.",
            "some text some textofficeaddress is.....",
            "OFF is OK",
            "test with OFF"
        };

        for (String s : strings) {
            System.out.println(s + " : " + pattern.matcher(s).find());
        }
    }
}

Upvotes: 1

Adam
Adam

Reputation: 36703

Just using [^\\p{Alpha}] around the OFF works, i.e. not a alphabetic character.

String [] inputs = {
    "some text you have 20% OFF. Avail @ this shop.",
    "some text some text office address is.....",
    "some text you have 20%OFF. Avail @ this shop." ,
    "some text you have 20%OFF! Avail @ this shop." ,
    "some text you have 20%OFF; Avail @ this shop.",
    "some text some textofficeaddress is....."};

for (String each : inputs) {
    System.out.println((each + " => " + each.matches(".*[^\\p{Alpha}]OFF[^\\p{Alpha}].*")));
}

Upvotes: 1

OneCricketeer
OneCricketeer

Reputation: 191701

Based on the vague description, this matches how you want.

No letters before or after OFF

[^a-zA-Z]OFF[^a-zA-Z]

Regex101 Demo

Though, I might suggest a number + percent + OFF.

\d+% OFF[^a-zA-Z]

Upvotes: 1

Related Questions