A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
sahid ul karim
Joined: Jun 06, 2007
Jun 16, 2008 04:06:00
I have a
like :- String str="This 'is' really good work";
i want to find 'is' . What should be the regex for this ?
Joined: Aug 05, 2005
Jun 16, 2008 04:11:00
Do you have to use regex ? str.indexOf("'is'") will do what you want.
Joined: Jun 01, 2007
Jun 16, 2008 07:10:00
The regex for 'is' is "'is'".
Joined: Feb 25, 2006
Jun 16, 2008 10:29:00
If it's the actual String "is" you're looking for, a simple indexOf(...) will do just fine, as already suggested.
If you want to capture text between ' (single quote), here's a way to do that:
String str = "This 'is' really good work"; Matcher m = Pattern.compile("(?<=')[^']+(?=')").matcher(str); System.out.println(m.find() ? m.group() : null);
Joined: May 06, 2004
Jun 16, 2008 20:15:00
That regex is more complicated than it needs to be, Piet--or at least, more advanced. The OP didn't even mention needing to extract the word from within the apostrophes, but if you really need to, using capturing groups is simpler:
String str = "This 'is' really good work"; Matcher m = Pattern.compile("'([^']+)'").matcher(str); System.out.println(m.find() ? m.group(1) : null);
Regular Expression Question
indexOf or Pattern Matcher faster/better?
Regular Expression: finding multiple lines
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014