What is the regular expression for this string?
-
I am looking for the regular expression for the following.
Microsoft.Sql/servers/*.*/databases
That can match
Microsoft.Sql/servers/some-text/databases
Microsoft.Sql/servers/some--other-text/databasesetc...
The words
"Microsoft.Sql/servers/"
&
"/databases"
should be an exact match.. and where
*.*
can match any text.
-
I am looking for the regular expression for the following.
Microsoft.Sql/servers/*.*/databases
That can match
Microsoft.Sql/servers/some-text/databases
Microsoft.Sql/servers/some--other-text/databasesetc...
The words
"Microsoft.Sql/servers/"
&
"/databases"
should be an exact match.. and where
*.*
can match any text.
Try this:
Microsoft.Sql/servers/.*/databases
The
.*
will match an character followed by any other character, so you may need to modify that if you want to exclude any specific characters (e.g. any that are not valid in path names). You can get yourself a free copy of Expresso Regular Expression Tool[^] which will help develop REs. [edit] As suggested by k5054 below, the RE should have anchors so it is restricted to the actual text starting atMicrosoft
and ending atdatabases
.^Microsoft.Sql/servers/.*/databases$
[/edit]
-
Try this:
Microsoft.Sql/servers/.*/databases
The
.*
will match an character followed by any other character, so you may need to modify that if you want to exclude any specific characters (e.g. any that are not valid in path names). You can get yourself a free copy of Expresso Regular Expression Tool[^] which will help develop REs. [edit] As suggested by k5054 below, the RE should have anchors so it is restricted to the actual text starting atMicrosoft
and ending atdatabases
.^Microsoft.Sql/servers/.*/databases$
[/edit]
-
You might want to add anchors to that, too eg:
^Microsoft.Sql/servers/.*/databases$
so you don't also match
my-Microsoft.Sql/servers/some-text/databases.info
Keep Calm and Carry On
-
I am looking for the regular expression for the following.
Microsoft.Sql/servers/*.*/databases
That can match
Microsoft.Sql/servers/some-text/databases
Microsoft.Sql/servers/some--other-text/databasesetc...
The words
"Microsoft.Sql/servers/"
&
"/databases"
should be an exact match.. and where
*.*
can match any text.
Member 15959121 wrote:
Microsoft.Sql/servers/*.*/databases
You are misusing the asterisk. It matches the preceding element only. which in this case is the forward slash. Moreover it matches zero or more. Which is probably not what you want. However you also said...
"Microsoft.Sql/servers/" and "/databases" should be an exact match
So the first attempt at a fix, which is not correct, would look like the following.
(Microsoft.Sql/servers/)?.*/databases
But that is limited then because it does not match the second expression. You might think the following is a good idea but do NOT do this. You should never create a regex in which everything is optional.
(Microsoft.Sql/servers/)?.*(/databases)?
You would need to use an or ('|') with 3 expressions (match first, match last, match all) which to me is way too confusing from the maintenance standpoint. It is not even clear to me if you have defined your match space. Presuming the following are NOT valid
Microsoft.Sql/servers/xxx
xxx/databasesThen I would do the following (pseudo code)
if match just: Microsoft.Sql/servers
else if match just: /databases
else match: Microsoft.Sql/servers/.*/databasesBut additionally note even the above matches the following which is probably not what you want.
Microsoft.Sql/servers///\\&4xz /databases/servers/databases