Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am in a situation where I need to grab the password value from configuration setting using regex.

actually I am using third party solution (cyberark password management) what it does is grab the password from config file and change the password.

To let cpm knows where is password in config file , I do some setting where I need to provide regex expression.

Working regex:
.*;Password=(.*)

Above regex works fine where configuration setting is like
value="Data Source=MyServer;Initial Catalog=MyDb;User ID=MeTestUser;Password=xyz123"

But it does not work for :
value="Data Source=MtTest.WORLD;Persist Security Info=True;User ID=MeTestApp;Password=abc123;Unicode=True"

For such case I provide
.*;Password=(.*;)
and still It does not work .
I need to make sure it only takes password value before semi-colon.

Note. CPM solution knows that its config file and so when it parse to get the value and change it looks for data between quotation.

What I have tried:

I tried
<pre>.*;Password=(.*;)
this regex expression but it did not work.
Posted
Updated 14-Sep-17 9:01am

Try
.*;Password=([^;"]*)
But that will not allow passwords containing a semicolon or a double quote.

Note that I have included the double quote as excluded character because with your first example that would be part of the match.
 
Share this answer
 
Comments
[no name] 13-Sep-17 11:19am    
It did not work. If you know Cyberark Password management system is solution to manage password. So what it does it (in my context) that it goes and look for the file and look for the key (these path for the file, key, value)is provided in CPM setting. If it finds Password in value it tries to change the password to new password.
Like i said .*;Password=(.*) this works only in the scenario where db connection string is like this
<add key="cnSQL" value="Data Source=MyServer;Initial Catalog=MyDb;User ID=MeTestUser;Password=xyz123" />

CPM knows double quote after password is not password value. so it goes n look for the string upto quote and change password.

But in connection string like
<add key="cnORA" value="Data Source=MtTest.WORLD;Persist Security Info=True;User ID=MeTestApp;Password=abc123;Unicode=True" />
above regex will not work as it thinks password is abc123;Unicode=True and try to change all of them..
This is my situation. I don't know if i made it clear or not. Sorry for my English.
Jochen Arndt 13-Sep-17 11:26am    
You can remove the double quote from the exclusion list.
I don't know the regex used by you.
But it is working with a common regex implementation (match any character except the semicolon).
[no name] 13-Sep-17 11:50am    
Thank you it worked.
Jochen Arndt 13-Sep-17 12:14pm    
Thank you for your feedback and accepting my solution.
[no name] 13-Sep-17 15:23pm    
sir quick question, how do I make ignore case on Password and any white space for example password = Mypassword;
Try this :
password\s*\=\s*(?<password>.*?)\s*[;\"]

Which will give you a named match of 'password' with the value of the actual password string.
 
Share this answer
 
Comments
[no name] 13-Sep-17 9:39am    
I tested that regex in http://regexpal.com.s3-website-us-east-1.amazonaws.com/?_ga=2.22655417.325949592.1505249668-1227866900.1505249668 it did not work. Could you please check.
Mehdi Gholam 13-Sep-17 9:42am    
What string are you testing with?
[no name] 13-Sep-17 9:43am    
db connection from config file..
"Data Source=MtTest.WORLD;Persist Security Info=True;User ID=MeTestApp;Password=abc123;Unicode=True"
[no name] 13-Sep-17 9:42am    
when I tested in https://regexr.com/ '?' in front of <password> as invalid target for quantifier.
Mehdi Gholam 13-Sep-17 9:44am    
Try this instead -> password\s*=\s*(.*?)\s*[;\"]
I would try this:
.*;Password=(.*)(;|")

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900