问个字符串截取提取的问题

missuo 2015-01-05 09:20:26
假如现在有一个数据库连接字符串:
CString strExample = _T(“server=localhost;database=db01; uid=sa;password=abc123;~other”);
我要从这个字符串中提取出password的值(即password=后面~之前的值),这个其实很简单。但是由于这个字符串中password可以写成大小写混杂,也可以写成pwd或者Pwd之类的(这样写ado都是可以支持的),还可以用“~”分割配置两个数据库连接,比如"server=localhost;database=db01; uid=sa;password=aBc123;~server=localhost;database=db03; User ID=test;PWd=xyXZ987;~other"。请问面对这些情况下,该怎么才能比较简单的提取出password的值呢?
...全文
421 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-01-06
  • 打赏
  • 举报
回复
CString Class Members Construction The String as an Array Assignment/Concatenation Comparison Extraction Other Conversions Searching Archive/Dump Buffer Access Windows-Specific Construction CString Constructs CString objects in various ways. The String as an Array GetLength Returns the number of characters in a CString object. For multibyte characters, counts each 8-bit character; that is, a lead and trail byte in one multibyte character are counted as two characters. IsEmpty Tests whether a CString object contains no characters. Empty Forces a string to have 0 length. GetAt Returns the character at a given position. operator [] Returns the character at a given position — operator substitution for GetAt. SetAt Sets a character at a given position. operator LPCTSTR Directly accesses characters stored in a CString object as a C-style string. Assignment/Concatenation operator = Assigns a new value to a CString object. operator + Concatenates two strings and returns a new string. operator += Concatenates a new string to the end of an existing string. Comparison operator == <, etc. Comparison operators (case sensitive). Compare Compares two strings (case sensitive). CompareNoCase Compares two strings (case insensitive). Collate Compares two strings (case sensitive, uses locale-specific information). CollateNoCase Compares two strings (case insensitive, uses locale-specific information). Extraction Mid Extracts the middle part of a string (like the Basic MID$ function). Left Extracts the left part of a string (like the Basic LEFT$ function). Right Extracts the right part of a string (like the Basic RIGHT$ function). SpanIncluding Extracts a substring that contains only the characters in a set. SpanExcluding Extracts a substring that contains only the characters not in a set. Other Conversions MakeUpper Converts all the characters in this string to uppercase characters. MakeLower Converts all the characters in this string to lowercase characters. MakeReverse Reverses the characters in this string. Replace Replaces indicated characters with other characters. Remove Removes indicated characters from a string. Insert Inserts a single character or a substring at the given index within the string. Delete Deletes a character or characters from a string. Format Format the string as sprintf does. FormatV Formats the string as vsprintf does. TrimLeft Trim leading whitespace characters from the string. TrimRight Trim trailing whitespace characters from the string. FormatMessage Formats a message string. Searching Find Finds a character or substring inside a larger string. ReverseFind Finds a character inside a larger string; starts from the end. FindOneOf Finds the first matching character from a set. Archive/Dump operator << Inserts a CString object to an archive or dump context. operator >> Extracts a CString object from an archive. Buffer Access GetBuffer Returns a pointer to the characters in the CString. GetBufferSetLength Returns a pointer to the characters in the CString, truncating to the specified length. ReleaseBuffer Releases control of the buffer returned by GetBuffer. FreeExtra Removes any overhead of this string object by freeing any extra memory previously allocated to the string. LockBuffer Disables reference counting and protects the string in the buffer. UnlockBuffer Enables reference counting and releases the string in the buffer. Windows-Specific AllocSysString Allocates a BSTR from CString data. SetSysString Sets an existing BSTR object with data from a CString object. LoadString Loads an existing CString object from a Windows resource. AnsiToOem Makes an in-place conversion from the ANSI character set to the OEM character set. OemToAnsi Makes an in-place conversion from the OEM character set to the ANSI character set. CString Overview | Hierarchy Chart
starytx 2015-01-06
  • 打赏
  • 举报
回复
写了一个函数,仅供参考
void FindPwd(CString str ,std::vector<CString> &vec)  // 将str中的密码字段内容保存在vec容器中
{
	int pos3 = 0;
	int pos1 = str.Find("=" ,pos3);
	int pos2 = str.Find(";" ,pos1+1);
	while (pos1 != -1)
	{
		CString szAttr = str.Mid(pos3 ,pos1 - pos3);
		CString szValue = str.Mid(pos1+1 ,pos2-pos1-1);
		szAttr.Trim();
		szValue.Trim();			// szValue可以不去空格,因为密码前后可能会有空格吧
		szAttr.MakeLower();		// 转换为小写
		if (szAttr.GetAt(0) == '~')
		{
			szAttr.Right(szAttr.GetLength()-1);
		}
		if (szAttr == "password" || szAttr == "pwd")
		{
			vec.push_back(szValue);
		}
		printf("%s = %s\n" ,szAttr ,szValue);	// 测试,打印出来看看
		pos3 = pos2+1;
		pos1 = str.Find("=" ,pos3);
		pos2 = str.Find(";" ,pos1+1);
	}
}


int _tmain(int argc, _TCHAR* argv[])
{
	CString strExample = _T("server=localhost;database=db01; uid=sa;password=abc123;~server=localhost;database=db03; User ID=test;PWd=xyXZ987;");

	std::vector<CString> vec;
	FindPwd(strExample ,vec);
	return 0;
}
starytx 2015-01-06
  • 打赏
  • 举报
回复
不复杂,有特征啊,比如每个参数都是分号分隔,还有等号之后的就是参数值,这些特征就差不多够了,将等号之前分号之后的参数名都转换为小写,然后和password或者pwd比较,就可以提取密码段了
幻夢之葉 2015-01-06
  • 打赏
  • 举报
回复
不会正则那也可以用简单的方式取得关键字如password,然后再取得=后的密码串吧(以;结尾)!
missuo 2015-01-06
  • 打赏
  • 举报
回复
引用 1 楼 missuo 的回复:
要考虑到大小写通用以及password和pwd可以互换的这个特征,感觉无从下手
引用 2 楼 pengzhixi 的回复:
找个正则高手应该不难
引用 3 楼 CharlesSimonyi 的回复:
用正则表达式容易实现,赶快投入到正则表达式的学习中把,以后经常用
没写过正则表达式,现在看看还来得及吧?
xionggch 2015-01-06
  • 打赏
  • 举报
回复
把字符串全部转换成大写或小写, 再比较password=或pwd=
赵4老师 2015-01-06
  • 打赏
  • 举报
回复
字符串……
赵4老师 2015-01-06
  • 打赏
  • 举报
回复
再字符串搜索提取问题域中,正则表达式不是万能的;有限状态自动机是万能的。 参考《编译原理》中的词法分析和有限状态自动机。
missuo 2015-01-06
  • 打赏
  • 举报
回复
引用 15 楼 starytx 的回复:
[quote=引用 14 楼 starytx 的回复:] [quote=引用 11 楼 missuo 的回复:] 谢谢你的那个函数啦,可以直接用,但是有点小问题,就是容易死循环,嘿嘿。
发现bug了,如果最后一个没有分号就会死循环,缺少对pos2的判断。在第9行前插入这个就行了。还有给15行前边加上 szAttr = .用意是去掉那个前缀破折号
		if (pos2 == -1)	// 没有找到分号,说明是最后一个参数,
		{
			pos2 = str.GetLength()-1;
		}
[/quote]一着急又写错了,pos2 = str.GetLength(); // 不能减一[/quote] 你看看对于这样的字符串该怎么分割呢? "Server=localhost;Database=DB501;User ID=sa;Password=c3Rhci4xM=~Server=localhost;Database=DB502;User ID=sa;Password=testpppp;~other..." 分割后第一个Password对应的应该是c3Rhci4xM=,第二个对应的是testpppp。
ri_aje 2015-01-06
  • 打赏
  • 举报
回复
正则表达式吧,c++11已经支持了。
starytx 2015-01-06
  • 打赏
  • 举报
回复
引用 14 楼 starytx 的回复:
[quote=引用 11 楼 missuo 的回复:] 谢谢你的那个函数啦,可以直接用,但是有点小问题,就是容易死循环,嘿嘿。
发现bug了,如果最后一个没有分号就会死循环,缺少对pos2的判断。在第9行前插入这个就行了。还有给15行前边加上 szAttr = .用意是去掉那个前缀破折号
		if (pos2 == -1)	// 没有找到分号,说明是最后一个参数,
		{
			pos2 = str.GetLength()-1;
		}
[/quote]一着急又写错了,pos2 = str.GetLength(); // 不能减一
starytx 2015-01-06
  • 打赏
  • 举报
回复
引用 11 楼 missuo 的回复:
谢谢你的那个函数啦,可以直接用,但是有点小问题,就是容易死循环,嘿嘿。
发现bug了,如果最后一个没有分号就会死循环,缺少对pos2的判断。在第9行前插入这个就行了。还有给15行前边加上 szAttr = .用意是去掉那个前缀破折号
		if (pos2 == -1)	// 没有找到分号,说明是最后一个参数,
		{
			pos2 = str.GetLength()-1;
		}
missuo 2015-01-06
  • 打赏
  • 举报
回复
引用 12 楼 starytx 的回复:
引用 11 楼 missuo 的回复:
谢谢你的那个函数啦,可以直接用,但是有点小问题,就是容易死循环,嘿嘿。
为啥死循环,给出你的用例
嗯,当等号和分号不配对的时候,比如: " Server=localhost;Database=DB501;User ID=sa;Password=c3Rhci4xM=~helloworld" 因为连接字符串的最后一个分号可以省略,所以还是要考虑这种情况的。 我为了方便,就直接在调用FindPwd函数之前把连接字符串格式化了一下,确保是等号和分号配对。
starytx 2015-01-06
  • 打赏
  • 举报
回复
引用 11 楼 missuo 的回复:
谢谢你的那个函数啦,可以直接用,但是有点小问题,就是容易死循环,嘿嘿。
为啥死循环,给出你的用例
missuo 2015-01-06
  • 打赏
  • 举报
回复
引用 8 楼 starytx 的回复:
不复杂,有特征啊,比如每个参数都是分号分隔,还有等号之后的就是参数值,这些特征就差不多够了,将等号之前分号之后的参数名都转换为小写,然后和password或者pwd比较,就可以提取密码段了
谢谢你的那个函数啦,可以直接用,但是有点小问题,就是容易死循环,嘿嘿。
encoderlee 版主 2015-01-05
  • 打赏
  • 举报
回复
用正则表达式容易实现,赶快投入到正则表达式的学习中把,以后经常用
pengzhixi 2015-01-05
  • 打赏
  • 举报
回复
找个正则高手应该不难
missuo 2015-01-05
  • 打赏
  • 举报
回复
要考虑到大小写通用以及password和pwd可以互换的这个特征,感觉无从下手

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧