简体   繁体   中英

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.

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]

Just using [^\\\\p{Alpha}] around the OFF works, ie 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}].*")));
}

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());
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM