• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Finding a pattern in a String

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My code takes in an array of Strings like:

String one = "t>*y";
String two = ">*";
String two = "he*ll>*o";
String two = "hell*othere";
String two = "hell>*othere";

I need to be able to check:
if the String contains ANY '*' that is not proceeded by '>' it will print that string.

Thus it will print he*ll>*o hell*othere because they contain an asterisks without the > proceeeding it.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could do this with regular expressions. But I'm guessing that you might find it easier to use some if/then statements along with a couple of String methods. In particular, you might look at the methods indexOf(String s) and charAt(int index).
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need the 'negative look behind' feature of regular expressions.

 
Dar Var
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great James that looks to work alright.

Can you explain the pattern String "(?<![>])\\*"

How does this work and how do you create it?

Have you any website address that explains this?
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can you explain the pattern String "(?<![>])\\*"



I could try to explain 'negative look behind' but you would do better looking at the Javadoc for class java.util.regex.Pattern and if that is not clear then use Google. I found http://computing.ee.ethz.ch/sepp/nedit-5.4-st/parenConstructs.html on the first page but there are bound to be others.
reply
    Bookmark Topic Watch Topic
  • New Topic