Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account
List archives

Multiline regex matches

Set property using part of a filename
(31 lines)
Conditional target problem
(42 lines)
Aug 17, 2010
Niklas Matthies
Niklas Matthies
Hi,

I need to fail a build if a certain file doesn't match a certain
multiline regex pattern. I tried somethink like

    <fail>
        <condition>
            <resourcecount when="equal" count="0">
                <restrict>
                    <fileset file="${file}"/>
                    <containsregexp expression="${regex}"/>
                </restrict>
            </resourcecount>
        </condition>
    </fail>

but unfortunately containsregexp doesn't perform multiline matches.
Specifying "(?m:${regex})" doesn't work.

If the <matches> condition would support resources, I could use
that,
but it only supports the string attribute.

Any other ideas?

-- Niklas Matthies


Reply
Messages in this thread
Multiline regex matches
reply Re: Multiline regex matches
(33 lines) Aug 17, 2010 12:38
reply Re: Multiline regex matches
(18 lines) Aug 17, 2010 13:11
reply Re: Multiline regex matches
(24 lines) Aug 17, 2010 13:37
reply Re: Multiline regex matches
(25 lines) Aug 17, 2010 13:46
Similar Threads
PATCH/puppet 1/1] [#4233] Ruby regexps are not multiline by default, but Resource titles can be mul
Puppet allows resource titles to contain newlines. We recently
introduced several regexps that were failing on resources with multiline
titles.

Signed-off-by: Jesse Wolfe <jes### @gmail.com>
---
 lib/puppet/resource/catalog.rb     |    2 +-
 lib/puppet/type.rb                 |    2 +-
 lib/puppet/type/file.rb            |    2 +-
 spec/unit/resource/catalog_spec.rb |    9 +++++++++
 spec/unit/type/file_spec.rb        |   14 ++++++++++++++
 spec/unit/type_spec.rb             |   21 +++++++++++++++++++++
 6 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/lib/puppet/resource/catalog.rb
b/lib/puppet/resource/catalog.rb
index d163fc1..4ac99ee 100644
--- a/lib/puppet/resource/catalog.rb
+++ b/lib/puppet/resource/catalog.rb
@@ -57,7 +57,7 @@ class Puppet::Resource::Catalog < Puppet::SimpleGraph
   end
 
   def title_key_for_ref( ref )
-    ref =~ /^(.+)\[(.*)\]/
+    ref =~ /^(.+)\[(.*)\]/m
     [$1, $2]
   end
 
diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb
index d0fea97..627271f 100644
--- a/lib/puppet/type.rb
+++ b/lib/puppet/type.rb
@@ -193,7 +193,7 @@ class Type
     when 0; []
     when 1;
       identity = lambda {|x| x}
-      [ [ /(.*)/, [ [key_attributes.first, identity ] ] ] ]
+      [ [ /(.*)/m, [ [key_attributes.first, identity ] ] ] ]
     else
       raise Puppet::DevError,"you must specify title patterns when there
are two or more key attributes"
     end
diff --git a/lib/puppet/type/file.rb b/lib/puppet/type/file.rb
index 195e8c8..71f2756 100644
--- a/lib/puppet/type/file.rb
+++ b/lib/puppet/type/file.rb
@@ -25,7 +25,7 @@ Puppet::Type.newtype(:file) do
     native resource to support what you are doing."
 
   def self.title_patterns
-    [ [ /^(.*?)\/?$/, [ [ :path, lambda{|x| x} ] ] ] ]
+    [ [ /^(.*?)\/*\Z/m, [ [ :path, lambda{|x| x} ] ] ] ]
   end
 
   newparam(:path) do
diff --git a/spec/unit/resource/catalog_spec.rb
b/spec/unit/resource/catalog_spec.rb
index b6f96f0..10cff91 100755
--- a/spec/unit/resource/catalog_spec.rb
+++ b/spec/unit/resource/catalog_spec.rb
@@ -1066,4 +1066,13 @@ describe Puppet::Resource::Catalog, "when
converting from pson" do
 
     lambda { PSON.parse @pson.to_pson }.should raise_error(ArgumentError)
   end
+
+  describe "#title_key_for_ref" do
+    it "should parse a resource ref string into a pair" do
+      @catalog.title_key_for_ref("Title[name]").should == ["Title",
"name"]
+    end
+    it "should parse a resource ref string into a pair, even if there's a
newline inside the name" do
+      @catalog.title_key_for_ref("Title[na\nme]").should == ["Title",
"na\nme"]
+    end
+  end
 end
diff --git a/spec/unit/type/file_spec.rb b/spec/unit/type/file_spec.rb
index 845bf3d..7d93dfd 100755
--- a/spec/unit/type/file_spec.rb
+++ b/spec/unit/type/file_spec.rb
@@ -1053,4 +1053,18 @@ describe Puppet::Type.type(:file) do
       file.retrieve
     end
   end
+
+  describe ".title_patterns" do
+    before do
+      @type_class = Puppet::Type.type(:file)
+    end
+    
+    it "should have a regexp that captures the entire string, except for
a terminating slash" do
+      patterns = @type_class.title_patterns
+      string = "abc/\n\tdef/"
+      patterns[0][0] =~ string
+      $1.should == "abc/\n\tdef"
+    end
+  end
+
 end
diff --git a/spec/unit/type_spec.rb b/spec/unit/type_spec.rb
index 683529d..71d415d 100755
--- a/spec/unit/type_spec.rb
+++ b/spec/unit/type_spec.rb
@@ -406,6 +406,27 @@ describe Puppet::Type do
     end
   end
 
+  describe ".title_patterns" do
+    describe "when there's one namevar" do
+      before do
+        @type_class = Puppet::Type.type(:notify)
+        @type_class.stubs(:key_attributes).returns([:one])
+      end
+
+      it "should have a default pattern for when there's one namevar" do
+        patterns = @type_class.title_patterns
+        patterns.length.should == 1
+        patterns[0].length.should == 2
+      end
+      
+      it "should have a regexp that captures the entire string" do
+        patterns = @type_class.title_patterns
+        string = "abc\n\tdef"
+        patterns[0][0] =~ string
+        $1.should == "abc\n\tdef"
+      end
+    end
+  end
 
   describe "when in a catalog" do
     before do






Using Regex
All,

 

I am using pig embedded in Java and need to use matches in my pig job.
However when I try to use escape characters in the pig line, the
compiler complains. How do I use complex regex while embedding?

 

Sample code that is throwing errors:

 

myServer.registerQuery("filtered = FILTER firstcut BY dIP matches
'\Q34.21.12.*\E';");

 

error: invalid escape sequence.

 

Thanks,

 

Matt

 



PATCH/puppet 1/1] [#4344] Fix for failing templates when module name matches file in local dir.
When the name of a module matches the name of a file in the local
directory, puppet agent would sometimes try to read that file and
interpret it as puppet code.  This happened because files.rb was
unintentionally permitting puppet files without an extension.  Fixed
by changing the glob pattern to only permit ".pp" and ".rb"
extensions.

Signed-off-by: Paul Berry <pa### @puppetlabs.com>
---
 lib/puppet/parser/files.rb           |    2 +-
 spec/unit/parser/files_spec.rb       |    6 +++---
 spec/unit/parser/type_loader_spec.rb |    2 +-
 test/language/parser.rb              |   12 ++++++------
 test/lib/puppettest.rb               |    4 ++--
 5 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/lib/puppet/parser/files.rb b/lib/puppet/parser/files.rb
index 9ef05e1..f346831 100644
--- a/lib/puppet/parser/files.rb
+++ b/lib/puppet/parser/files.rb
@@ -24,7 +24,7 @@ module Puppet::Parser::Files
       # Than that would be a "no."
     end
     abspat = File::expand_path(start, cwd)
-    [nil, Dir.glob(abspat + (File.extname(abspat).empty? ? '{,.pp,.rb}' :
'' )).uniq.reject { |f| FileTest.directory?(f) }]
+    [nil, Dir.glob(abspat + (File.extname(abspat).empty? ? '{.pp,.rb}' :
'' )).uniq.reject { |f| FileTest.directory?(f) }]
   end
 
   # Find the concrete file denoted by +file+. If +file+ is absolute,
diff --git a/spec/unit/parser/files_spec.rb
b/spec/unit/parser/files_spec.rb
index fcfbfa6..3eb0db0 100644
--- a/spec/unit/parser/files_spec.rb
+++ b/spec/unit/parser/files_spec.rb
@@ -154,7 +154,7 @@ describe Puppet::Parser::Files do
 
     it "should match against provided fully qualified patterns" do
       pattern = @basepath + "/fully/qualified/pattern/*"
-      Dir.expects(:glob).with(pattern+'{,.pp,.rb}').returns(%w{my file
list})
+      Dir.expects(:glob).with(pattern+'{.pp,.rb}').returns(%w{my file
list})
       Puppet::Parser::Files.find_manifests(pattern)[1].should == %w{my
file list}
     end
 
@@ -168,7 +168,7 @@ describe Puppet::Parser::Files do
       pattern = @basepath + "/fully/qualified/pattern/*"
       file = @basepath + "/my/file"
       dir = @basepath + "/my/directory"
-      Dir.expects(:glob).with(pattern+'{,.pp,.rb}').returns([file, dir])
+      Dir.expects(:glob).with(pattern+'{.pp,.rb}').returns([file, dir])
       FileTest.expects(:directory?).with(file).returns(false)
       FileTest.expects(:directory?).with(dir).returns(true)
       Puppet::Parser::Files.find_manifests(pattern)[1].should == [file]
@@ -176,7 +176,7 @@ describe Puppet::Parser::Files do
 
     it "should return files once only" do
       pattern = @basepath + "/fully/qualified/pattern/*"
-      Dir.expects(:glob).with(pattern+'{,.pp,.rb}').returns(%w{one two
one})
+      Dir.expects(:glob).with(pattern+'{.pp,.rb}').returns(%w{one two
one})
       Puppet::Parser::Files.find_manifests(pattern)[1].should == %w{one
two}
     end
   end
diff --git a/spec/unit/parser/type_loader_spec.rb
b/spec/unit/parser/type_loader_spec.rb
index 8f005d5..83006b3 100644
--- a/spec/unit/parser/type_loader_spec.rb
+++ b/spec/unit/parser/type_loader_spec.rb
@@ -192,7 +192,7 @@ describe Puppet::Parser::TypeLoader do
   end
 
   it "should be able to add classes to the current resource type
collection" do
-    file = tmpfile("simple_file")
+    file = tmpfile("simple_file.pp")
     File.open(file, "w") { |f| f.puts "class foo {}" }
     @loader.import(file)
 
diff --git a/test/language/parser.rb b/test/language/parser.rb
index 5a433c7..8cda8ee 100755
--- a/test/language/parser.rb
+++ b/test/language/parser.rb
@@ -97,7 +97,7 @@ class TestParser < Test::Unit::TestCase
     }
 
     4.times { |i|
-      path = File.join(basedir, subdir, "subfile#{i}")
+      path = File.join(basedir, subdir, "subfile#{i}.pp")
       mkmanifest(path)
     }
 
@@ -137,8 +137,8 @@ class TestParser < Test::Unit::TestCase
   end
 
   def test_importedclasses
-    imported = tempfile
-    importer = tempfile
+    imported = tempfile '.pp'
+    importer = tempfile '.pp'
 
     made = tempfile
 
@@ -655,9 +655,9 @@ file { "/tmp/yayness":
   end
 
   def test_multiple_imports_on_one_line
-    one = tempfile
-    two = tempfile
-    base = tempfile
+    one = tempfile '.pp'
+    two = tempfile '.pp'
+    base = tempfile '.pp'
     File.open(one, "w") { |f| f.puts "$var = value" }
     File.open(two, "w") { |f| f.puts "$var = value" }
     File.open(base, "w") { |f| f.puts "import '#{one}', '#{two}'" }
diff --git a/test/lib/puppettest.rb b/test/lib/puppettest.rb
index e31a319..294d0ef 100755
--- a/test/lib/puppettest.rb
+++ b/test/lib/puppettest.rb
@@ -227,14 +227,14 @@ module PuppetTest
     #Facter.stubs(:to_hash).returns({})
   end
 
-  def tempfile
+  def tempfile(suffix = '')
     if defined?(@@tmpfilenum)
       @@tmpfilenum += 1
     else
       @@tmpfilenum = 1
     end
 
-    f = File.join(self.tmpdir, "tempfile_" + @@tmpfilenum.to_s)
+    f = File.join(self.tmpdir, "tempfile_" + @@tmpfilenum.to_s + suffix)
     @@tmpfiles ||= []
     @@tmpfiles << f
     f






Ask a question about regex in CRS
Hi, everyone
    The following rule comes from
rules/base_rules/modsecurity_crs_41_sql_injection_attacks.conf , but I
don't understand what does the regular expression "(?:[\\\(\)\%#]|--)"
mean. What's the meaning of "\%" in a regex?

SecRule MATCHED_VAR "(?:[\\\(\)\%#]|--)"
        
"t:none,setvar:'tx.msg=%{rule.msg}',setvar:tx.sql_injection_score=+%{tx.critical_anomaly_score},setvar:tx.anomaly_score=+%{tx.critical_anomaly_score},setvar:tx.%{rule.id}-WEB_ATTACK/SQL_INJECTION-%{matched_var_name}=%{tx.0}"


Using variables in regex
Well, how do I use the content of a variable in regex?

$username = "user1"
file {  "userdata.tar.bz2":
                source => "puppet://$server/modules/$module/
userdata.tar.bz2",
                ensure => $users ? {
                                /$username/ => absent,
                                default => present,
                        },
}

$users is a custom fact that contains all local users:

users => at avahi bin daemon dnsmasq ftp games haldaemon lp mail
messagebus nobody ntp polkituser postfix pulse root sshd suse uuidd
wwwrun man news uucp puppet user1

When I hardcode "user1" into the regex my test works fine and the file
is removed.

But things like /$variable/ or /\$variable/ or /#{variable}/ just
don't work.
Is it even possible in version 0.25.4?





Using Regex in Embedded Pig in Java
All,

 

I am using pig embedded in Java and need to use matches in my pig job.
However when I try to use escape characters in the pig line, the
compiler complains. How do I use complex regex while embedding?

 

Sample code that is throwing errors:

 

myServer.registerQuery("filtered = FILTER firstcut BY dIP matches
'\Q34.21.12.*\E';");

 

error: invalid escape sequence.

 

Thanks,

 

Matt



Issues with Node Regex

I am trying to match groups of nodes - i.e.

Node: synd1-path2.path2.some.domain
Node: synd2-path2.path2.some.domain

By using either of the node definitions below:

node /^synd\w+\.path2\.some\.domain$/ {
    include ibapps
    include db
}


A question about android regex implementation
Hi  Jesse and All,
I have written some simple benchmarks for harmony regex and find the
performance of harmony is poor compared to RI. For example, Mathcer.find()
only reach 60% of that of RI. I heard Android use icu4jni re-implement
this
module. Since icu4jni use native code I think it may has higher
performance
than harmony. I am trying to use icu4jni as the back-end of harmony regex
but find icu4jni has no functions related to regex operations.
I know there are some android guys in our community. So can anyone tell me
some detail info for android's regex, like if it re-implement the regex
logic using native code by android itself rather than icu4jni and really
get
higher performance compared to harmony regex? Thanks a lot!


client-side password validation using regex
Hi all,

I was wondering if it's possible to validate a <html:password> field
on client-side by using a regular expression.
A look at validateMask.js told me that the only field types supported are
'hidden', 'text', 'textarea' and 'file'.

Code:

            if ((field.type == 'hidden' ||
                field.type == 'text' ||
                 field.type == 'textarea' ||
                                 field.type == 'file') &&
                 (field.value.length > 0)) {

                if (!jcv_matchPattern(field.value, oMasked[x][2]("mask")))
{
                    if (i == 0) {
                        focusField = field;
                    }
                    fields[i++] = oMasked[x][1];
                    isValid = false;
                }
            }


Is this the exspected behavior? Is there any reason for not supporting
'password' fields?
If so, how would I validate a password field by using a regex (without
changing the js file on my own :))?

Cheers,


Ref. 324 * Geoinformationszentrum
Tel. 0211 9449-6310 * Fax: 0211 9449-6610
Email: stephan.### @it.nrw.de<mailto:stephan.k### @it.nrw.de>



Created: (HIVE-1483) Update AWS S3 log format deserializer regex
Update AWS S3 log format deserializer regex

DO NOT REPLY access allowed if password matches first seven characters of real password
https://issues.apache.org/bugzilla/show_bug.cgi?id=46152

Nick Kew <nic### @webthing.com> changed:

           What    |Removed                     |Added