// -*- c++ -*- /* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: RegexTest.jlc,v 1.2 2003/09/26 15:55:26 florian Exp $ */ #include "Test.h" #include #include using namespace jakelib::lang; using namespace jakelib::util; using namespace jakelib::util::regex; #pragma javasyntax /*****************************************************************************\ * tryRegex | *****************************************************************************/ void tryRegex(String* pattern, String* input) { System::out->print(`"p = Pattern::compile("`); if (pattern == null) System::out->print(`"null)\n"`); else System::out->print(`"\""` .. pattern .. `"\")\n"`); try { Pattern* p = Pattern::compile(pattern); Matcher* m = p->matcher(input); System::out->println(`" p->matcher()->matches(\""` .. input .. `"\") : "` .. m->matches()); while (m->find()) { System::out->print(`" "`); for (int idx = 0; idx <= m->groupCount(); idx++) { System::out->print(`" (start = "` .. m->start(idx) .. `"; end = "` .. m->end(idx) .. `"; '"` .. m->group(idx) .. `"')"`); } System::out->println(); } } catch (Exception *ex) { System::out->println(`" --> "` .. ex); } } /*****************************************************************************\ * tryRegexSplit | *****************************************************************************/ void tryRegexSplit(String* pattern, String* input) { System::out->print(`"p = Pattern::compile("`); if (pattern == null) System::out->print(`"null)\n"`); else System::out->print(`"\""` .. pattern .. `"\")\n"`); try { Pattern* p = Pattern::compile(pattern); System::out->print(`" l = p->split(\""` .. input .. `"\")\n"`); ArrayList* l = p->split(input); System::out->println(`" => "` .. l); } catch (Exception *ex) { System::out->println(`" --> "` .. ex); } } /*****************************************************************************\ * tryRegexReplace | *****************************************************************************/ void tryRegexReplace(String* pattern, String* input, String* replacement) { System::out->print(`"p = Pattern::compile("`); if (pattern == null) System::out->println(`"(null)"`); else System::out->print(`"\""` .. pattern .. `"\")\n"`); try { Pattern* p = Pattern::compile(pattern); Matcher* m = p->matcher(input); System::out->println(`" m = p->matcher(\""` .. input .. `"\")"`); String* str = m->replaceAll(replacement); System::out->println(`" replacement: '"` .. replacement .. `"' => '"` .. str .. `"'"`); } catch (Exception *ex) { System::out->println(`" --> "` .. ex); } } /*****************************************************************************\ * regexTest | *****************************************************************************/ void regexTest() { tryRegex(`"(-)?(([0-9]+)\\.?([0-9]*))"`, `"-90.887"`); tryRegex(`"(-)?(([0-9]+)\\.?([0-9]*))"`, `" +90.887"`); tryRegex(`"(-)?[0-9]+\\.?[0-9]*"`, `"hello"`); tryRegex(`"(red|green|blue)"`, `"yellow"`); tryRegex(`"(red|green|blue)"`, `"blue"`); tryRegex(`"-(\\d+)-"`, `"11--222--3333--4444--"`); tryRegex(`"<.*?>"`, `"foo bar"`); tryRegex(`"(((aaaaa)"`, `""`); tryRegex(null, `""`); tryRegexSplit(`"\\s*;\\s*"`, `"This; is; just;;;a; Test;!;!!"`); tryRegexSplit(`"\\s*;\\s*"`, `"This is just a Test!"`); tryRegexReplace(`"cat"`, `"one cat two cats in the yard"`, `"dog"`); tryRegexReplace(`"\\d+"`, `"1024; -1234; 31415; 2002"`, `"$$($0)"`); tryRegexReplace(`"\\d+"`, `"1024; -1234; 31415; 2002"`, `"($1)"`); }