Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string ends with certain pattern

If I have a string like:

This.is.a.great.place.too.work. 

or:

This/is/a/great/place/too/work/ 

than my program should give me that the sentence is valid and it has "work".


If I Have :

This.is.a.great.place.too.work.hahahha 

or:

This/is/a/great/place/too/work/hahahah 

then my program should not give me that there is a "work" in the sentence.


So I am looking at java strings to find a word at the end of the sentence having . or , or / before it. How can I achieve this?

like image 420
The Learner Avatar asked Sep 07 '12 02:09

The Learner


2 Answers

This is really simple, the String object has an endsWith method.

From your question it seems like you want either /, , or . as the delimiter set.

So:

String str = "This.is.a.great.place.to.work.";  if (str.endsWith(".work.") || str.endsWith("/work/") || str.endsWith(",work,"))      // ...  

You can also do this with the matches method and a fairly simple regex:

if (str.matches(".*([.,/])work\\1$")) 

Using the character class [.,/] specifying either a period, a slash, or a comma, and a backreference, \1 that matches whichever of the alternates were found, if any.

like image 58
pb2q Avatar answered Sep 28 '22 06:09

pb2q


You can test if a string ends with work followed by one character like this:

theString.matches(".*work.$"); 

If the trailing character is optional you can use this:

theString.matches(".*work.?$"); 

To make sure the last character is a period . or a slash / you can use this:

theString.matches(".*work[./]$"); 

To test for work followed by an optional period or slash you can use this:

theString.matches(".*work[./]?$"); 

To test for work surrounded by periods or slashes, you could do this:

theString.matches(".*[./]work[./]$"); 

If the tokens before and after work must match each other, you could do this:

theString.matches(".*([./])work\\1$"); 

Your exact requirement isn't precisely defined, but I think it would be something like this:

theString.matches(".*work[,./]?$"); 

In other words:

  • zero or more characters
  • followed by work
  • followed by zero or one , . OR /
  • followed by the end of the input

Explanation of various regex items:

.               --  any character *               --  zero or more of the preceeding expression $               --  the end of the line/input ?               --  zero or one of the preceeding expression [./,]           --  either a period or a slash or a comma [abc]           --  matches a, b, or c [abc]*          --  zero or more of (a, b, or c) [abc]?          --  zero or one of (a, b, or c)  enclosing a pattern in parentheses is called "grouping"  ([abc])blah\\1  --  a, b, or c followed by blah followed by "the first group" 

Here's a test harness to play with:

class TestStuff {      public static void main (String[] args) {          String[] testStrings = {                  "work.",                 "work-",                 "workp",                 "/foo/work.",                 "/bar/work",                 "baz/work.",                 "baz.funk.work.",                 "funk.work",                 "jazz/junk/foo/work.",                 "funk/punk/work/",                 "/funk/foo/bar/work",                 "/funk/foo/bar/work/",                 ".funk.foo.bar.work.",                 ".funk.foo.bar.work",                 "goo/balls/work/",                 "goo/balls/work/funk"         };          for (String t : testStrings) {             print("word: " + t + "  --->  " + matchesIt(t));         }     }      public static boolean matchesIt(String s) {         return s.matches(".*([./,])work\\1?$");     }      public static void print(Object o) {         String s = (o == null) ? "null" : o.toString();         System.out.println(o);     }  } 
like image 36
jahroy Avatar answered Sep 28 '22 07:09

jahroy