• 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

Regular expression syntax

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please look at the code below which searches for the occurence of a set of Strings in a main String.
Is there a way to eliminate the for loop below and use an And clause (&&) instead?

groupArray contains the groups I want to search for in mainString. For example, groupArray[0] = "String0", groupArray[1] = "String1", etc.

Venita
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is just a bit confusing.
You have a class Pattern that has a matcher() method which returns what? It returns a Matcher class? Than you call the find() method on that class.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by William Barnes:
This is just a bit confusing.
You have a class Pattern that has a matcher() method which returns what? It returns a Matcher class? Than you call the find() method on that class.


What's confusing about that? Does a look at http://java.sun.com/j2se/1.4.1/docs/api/java/util/regex/package-summary.html help?
What's confusing me is why you would want an AND operator for regexp. Venita, what are your requirements???
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Does a look at http://java.sun.com/j2se/1.4.1/docs/api/java/util/regex/package-summary.html help?


Well I guess so. Since I hadn't heard of these classes/api before.
And looking at them I would suggest that the original poster might want to use "matches()" instead of "find()" for starters.
But in either case a for loop doesn't seem to make sense. From the small code snipet I see so far.
[ October 30, 2002: Message edited by: William Barnes ]
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The difference between matches() and find() are how they treat the regex. matches() has to match the entire string, where find() just matches some part of the string.
The code you have seems to ensure that the mainString contains all the patterns in the groupArray, in any order or with any type of overlaping.
So if groupArray = { "a", ".b." } and the main string were "abc" it would return true, even though the substrings matching "a" and ".b." overlap. But mainArray = "bbb" would not be true since it does not match all of the groupArray expressions.
If that's what you are trying to do, then I don't know a way to write that general regex given all the groupArray elements.
If groupArray is not going to contain regex, but is just substrings then you should probably skip the regex thing and stick with String.indexOf() != -1. Probably faster (not sure about this) but easier to read. It is also less prone to failure if someone were to stick regex special symbols in the groupArray.
 
Venita Glasfurd
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If groupArray is not going to contain regex, but is just substrings then you should probably skip the regex thing and stick with String.indexOf() != -1. Probably faster (not sure about this) but easier to read. It is also less prone to failure if someone were to stick regex special symbols in the groupArray.


Thanks Dave.. yes, my groupArray is not going to contain regex. So, I will go with the simple indexOf method.
Venita.
 
reply
    Bookmark Topic Watch Topic
  • New Topic