1 /** 2 Copyright: Copyright (c) 2017, Joakim Brännström. All rights reserved. 3 License: MPL-2 4 Author: Joakim Brännström (joakim.brannstrom@gmx.com) 5 6 This Source Code Form is subject to the terms of the Mozilla Public License, 7 v.2.0. If a copy of the MPL was not distributed with this file, You can obtain 8 one at http://mozilla.org/MPL/2.0/. 9 10 This file contains helper functions for matching with regex. 11 */ 12 module dextool.plugin.regex_matchers; 13 14 import std.regex : Regex; 15 16 /// Returns: true if the value match any regex. 17 bool matchAny(const string value, Regex!char[] re) @safe nothrow { 18 import std.algorithm : canFind; 19 import std.regex : matchFirst, RegexException; 20 21 bool passed = false; 22 23 foreach (ref a; re) { 24 try { 25 auto m = matchFirst(value, a); 26 if (!m.empty && m.pre.length == 0 && m.post.length == 0) { 27 passed = true; 28 break; 29 } 30 } 31 catch (RegexException ex) { 32 } 33 catch (Exception ex) { 34 } 35 } 36 37 return passed; 38 } 39 40 version (unittest) { 41 import unit_threaded : shouldBeTrue; 42 } 43 44 @("Shall match all regex") 45 @safe unittest { 46 import std.regex : regex; 47 48 Regex!char[] re = [regex(".*/foo/.*"), regex(".*/src/.*")]; 49 50 matchAny("/p/foo/more/src/file.c", re).shouldBeTrue; 51 }