afriedman111
afriedman111

Reputation: 2351

Reg Exp not matching

I have a regular expression that is not finding a match with text that is in my file

Reg Ex:

^[ \t]*#[ \t]+vtk[ \t]+DataFile[ \t]+Version[ \t]+([^\s]+)[ \t]*\n(.*)\n[ \t]*(ASCII|BINARY)[ \t]*\n[ \t]*DATASET[ \t]+([^ ]+)[ \t]*\n

File text:

# vtk DataFile Version 4.2
ASCII
DATASET

When I cut off the expression to the following it works:

 ^[ \t]*#[ \t]+vtk[ \t]+DataFile[ \t]+Version[ \t]+([^\s]+)[ \t]*\n(.*)\n[ \t]*

Why is the text not being matched?

Upvotes: 1

Views: 75

Answers (1)

The fourth bird
The fourth bird

Reputation: 163632

I think you are matching (.*)\n too many and after DATASET there is no more data to match but in your pattern there is still [ \t]+([^ ]+)[ \t]*\n which are not optional.

Try it like this:

^[ \t]*#[ \t]+vtk[ \t]+DataFile[ \t]+Version[ \t]+([^\s]+)[ \t]*\n[ \t]*(ASCII|BINARY)[ \t]*\n[ \t]*DATASET

In parts, your pattern would look like:

  • ^
  • [ \t]*#
  • [ \t]+vtk
  • [ \t]+DataFile
  • [ \t]+Version
  • [ \t]+([^\s]+) This group will match the 4.2
  • [ \t]*\n
  • [ \t]*(ASCII|BINARY)
  • [ \t]*\n
  • [ \t]*DATASET

Regex demo

Upvotes: 2

Related Questions