// This will strip out any punctuation and spaces from filenames, replacing such characters with underscores
$filename = preg_replace("/[^a-zA-Z0-9s.]/", "_", $filename);
Tag Archive for expressions
Regular expressions filename replace
Regular Expressions 3/3
//pattern examples. Creating a time pattern that can be reused.
def timePattern = ~/dd:dd/
def timeString = "Between 06:00 and 10:00, most geeks are asleep."
def matcher = timePattern.matcher(timeString)
def times = []
matcher.each { times << it }
assert times.join(", ") == '06:00, 10:00'
def anotherTimeString = "But between 22:00 and 02:00, most geeks are awake"
//we reuse the pattern
matcher = timePattern.matcher(anotherTimeString)
times = []
matcher.each { times << it }
assert times.join(", ") == '22:00, 02:00'
Regular Expressions 2/3
//examples for match operator - match is more restrictive than find! def line = "This is a test line" assert line ==~ /This is a test line/ line -= " line" assert !(line ==~ /This is a test line/) def booleanValue = (line ==~ /This is a test/) assert booleanValue == true
Regular Expressions 1/3
//regex patterns look strange, but groovy makes them look better
//use slashy syntax to declare a pattern, get rid of double-esacping
//example below both defines the same pattern to match a time like 15:01
assert "dd:dd" == /dd:dd/
//find a string, return true if found, false otherwise
assert 'The time is 15:01' =~ /dd:dd/
//note, you can easily use this for branching
def timeString = 'It is now 17:34'
def timeRegex = /dd:dd/
if (timeString =~ timeRegex) assert true
else assert false
//calling matcher.find() finds the first occurrence, then steps to the next if called again
//notice we used a regex group in the pattern: /(dd:dd)/
def matcher = ("It is 15:01, it is 15:02, it is 15:03" =~ /(dd:dd)/ )
assert matcher instanceof java.util.regex.Matcher
def times = []
while (matcher.find())
{
times << matcher.group() //this adds each match to the list
}
assert times.size() == 3
assert times.join(",") == "15:01,15:02,15:03"
//a more groovy way to the same thing as above, eachMatch method in String
//note: no grouping parentheses needed in regex! /dd:dd/
times = []
"It is 15:01, it is 15:02, it is 15:03".eachMatch(/dd:dd/) {
times << it[0] //it is a list, position 0 contains the complete match
}
assert times.size() == 3
assert times.join(",") == "15:01,15:02,15:03"
//using groups with the matcher and each closure. Within the pattern, we can use GString replacements to make
//the pattern readable
def DATE = /dd.dd.dddd/
def TIME = /dd:dd/
def dates = []
("Today is 31.01.2007, 15:01. Tomorrow is 01.02.2007, 15:01" =~ /($DATE), ($TIME)/).each { all, date, time ->
dates << date
}
assert dates.size() == 2
assert dates.join(', ') == '31.01.2007, 01.02.2007'
Regular Expressions simple strings search/replace escape method (with RE speed enhancment – precompilation)
RegExp.escape = function(text) {
if (!arguments.callee.sRE) {
var specials = [
'/', '.', '*', '+', '?', '|',
'(', ')', '[', ']', '{', '}', '\'
];
arguments.callee.sRE = new RegExp(
'(\' + specials.join('|\') + ')', 'g'
);
}
return text.replace(arguments.callee.sRE, '\$1');
}
Category: Uncategorized |
Tags: escape, expressions, match, pattern, precompile, regular, replace, speed, text