3

I have the following regular expression pattern that matches fully qualified Microsoft SQL Server table names ([dbName].[schemaName].[tableName]), where the schema name is optional:

val tableNamePattern = """\[(\w+)\](?:\.\[(\w+)\])?\.\[(\w+)\]""".r

I am using it like this:

val tableNamePattern(database, schema, tableName) = fullyQualifiedTableName

When the schema name is missing (e.g.: [dbName].[tableName]), the schema value gets set to null.

Is there a Scala idiomatic way to set it to None instead, and to Some(schema) when the schemaName is provided?

Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems. -- Jamie Zawinski

4

1 に答える 1