如何使用表达式从配置文件中仅获取密码值? [英] How to grab only password value from config file using expression?

查看:51
本文介绍了如何使用表达式从配置文件中仅获取密码值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处于需要使用正则表达式从配置设置中获取密码值的情况。



实际上我使用的是第三方解决方案(cyberark密码管理),它的作用是从配置文件中获取密码并更改密码。



为了让cpm知道配置文件中的密码在哪里,我做了一些我需要提供正则表达式的设置。



工作正则表达式:

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=(.*)



以上正则表达式在配置设置类似的情况下正常工作

value =数据源= MyServer;初始目录= MyDb;用户ID = MeTestUser;密码= xyz123



但它不起作用:

value =数据源= MtTest.WORLD;持久安全信息=真;用户ID = MeTestApp;密码= abc123; Unicode =真



对于这种情况,我提供


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=(.*;)

但仍然不起作用。

我需要确保它在分号前只需要密码值。



注意。 CPM解决方案知道它的配置文件,所以当它解析获取值并更改它时会在引用之间查找数据。



我尝试过:



我试过

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.

推荐答案

尝试

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.


试试这个:

Try this :
password\s*\=\s*(?<password>.*?)\s*[;\"]



哪个会给你一个命名匹配'password',带有实际密码字符串的值。


Which will give you a named match of 'password' with the value of the actual password string.


我会试试这个:

I would try this:
.*;Password=(.*)(;|")



只是一个fe有趣的链接,以帮助构建和调试RegEx。

以下是RegEx文档的链接:

perlre - perldoc.perl.org [ ^ ]

以下是帮助构建RegEx并对其进行调试的工具的链接:

.NET Regex Tester - Regex Storm [ ^ ]

Expresso正则表达式工具 [ ^ ]

RegExr:Learn,Build,&测试RegEx [ ^ ]

此节目RegEx是一个很好的图表,它非常有助于理解RegEx的作用:

Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]


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.[^]


这篇关于如何使用表达式从配置文件中仅获取密码值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆