commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stephen Colebourne <scolebou...@joda.org>
Subject Re: [lang] Wildcard regex
Date Fri, 08 Oct 2010 14:32:58 GMT
Human users enter wildcards * and ? (because regex is too complex). In
my case, I'm passing it to MongoDB, which needs regex.

Stephen


On 8 October 2010 15:10, Paul Benedict <pbenedict@apache.org> wrote:
> Can I get some sense of use case? What would you use it for? Just curious.
>
> On Fri, Oct 8, 2010 at 9:06 AM, Stephen Colebourne <scolebourne@joda.org> wrote:
>> I don't think comons lang has a routine for converting a standard
>> wildcard string (with * and ?) to a regex.
>> Here is a first suggestion, although I'm sure it can be improved.
>>
>>  public Pattern createPattern(String text) {
>>    StringTokenizer tkn = new StringTokenizer(text, "?*", true);
>>    StringBuilder buf = new StringBuilder(text.length() + 10);
>>    buf.append('^');
>>    boolean lastStar = false;
>>    while (tkn.hasMoreTokens()) {
>>      String str = tkn.nextToken();
>>      if (str.equals("?")) {
>>        buf.append('.');
>>        lastStar = false;
>>      } else if (str.equals("*")) {
>>        if (lastStar == false) {
>>          buf.append(".*");
>>        }
>>        lastStar = true;
>>      } else {
>>        buf.append(Pattern.quote(str));
>>        lastStar = false;
>>      }
>>    }
>>    buf.append('$');
>>    return Pattern.compile(buf.toString(), Pattern.CASE_INSENSITIVE);
>>  }
>>
>> Other possile conversions would be * and ? to databse wildcards, so
>> perhaps there is scope for a few related methods here?
>>
>> Stephen
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message