Why does this space matter in this regular expression?

Why does the space make all the difference?

select * from beds where id~'.*Extra large.* (Red).*';

and

select * from beds where id~'.*Extra large.*(Red).*';

The first one returned nothing and the second acted as I wanted. An example of what I want matched is:

"Extra large" (Red) {2012 model}

I thought the first would work since there is a space after (Red)?

EDIT:Even if I escape the brackets with '\' I still can't have a space there.

Answers


The problem is that you have not escaped your brackets around "Red". Your regex should be:

'.*Extra large.* \(Red\).*'

This makes the brackets literal brackets, but without escaping them they create a regex group (and not characters to be matched).

Your first regex grouped the characters Red and required a space to precede that group Red, so it would match "... Red...", but there is a bracket in your input before Red, so it doesn't match.

Your second regex accepts any character(s) (via .*) before Red, so it matches.


This is because you're not escaping the ().

The brackets around "Red" create a group and are not included in the match. This is the reason why the regexp without the whitespace works.

The .* in the regexp without the whitespace matches " (, then comes Red and after that ) {2012 model}. The brackets are matched by the .* operators.

The .* in the regexp with the whitespace matches " and the ( is not included in the pattern.

So the right pattern would be this:

.*Extra large.*\(Red\).*

Need Your Help

how to map wgs84 coordinates to sql server b-tree index (w/o using built in spatial indexes)

sql-server geometry coordinate-systems wgs84 tessellation

I would like to be able to map geographic points from WGS84, I believe, formatted for ms sql server, to the set of cells it would touch if the same coordinate pair were to be tessallated into an sql

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.