简体   繁体   English

Java中的正则表达式字符串

[英]Regular expression string in java

I want to explain my problem about a command string submitted from the client to the server. 我想解释一下有关从客户端提交到服务器的命令字符串的问题。 Through this specific command I have to login into the database managed by the server. 通过此特定命令,我必须登录到服务器管理的数据库。 According to the project's guide lines, command's formatting is: login@username;password. 根据项目的准则,命令的格式为:login @ username; password。 The string is sent to the server through a socket. 该字符串通过套接字发送到服务器。 User inserts his credentials and these are sent to the server putting them into the command. 用户插入其凭据,并将这些凭据发送到服务器,将其放入命令中。 The server has to split the user and the password from the command and has to check, in the database, if the user already exists. 服务器必须从命令中分割用户和密码,并且必须在数据库中检查用户是否已经存在。 Here is my problem, given the fact that in the user and in the password can be present ; 考虑到可以在用户名和密码中出现的事实,这是我的问题; (the same character of the separator), I don't understand (server side) how can i divide user and psw from the command so that user and psw are the same of the ones inserted in the client. (分隔符的相同字符),我不理解(服务器端)如何从命令中划分用户和psw,以便用户和psw与插入客户端的用户和psw相同。

Have you got some ideas about? 你有什么想法吗? I was thinking to use regular expression, is this the right way? 我当时想使用正则表达式,这是正确的方法吗?

I would just split the string into a user/pass string like this: 我只是将字符串分成这样的用户/传递字符串:

String userPass = "User;Pass";
String[] parts = userPass.split(";");
String user = parts[0];
String pass = parts[1];

If you really have to split a string by a separator included in both substrings there is no way to make sure the string is always split correctly! 如果确实需要通过两个子字符串中包含的分隔符来分隔字符串,则无法确保始终正确地分隔字符串!

I think you can use the unicode unit separator character 0x001F to separate you strings safely, as the user will have some difficulties entering such a character! 我认为您可以使用unicode 单位分隔符0x001F来安全地分隔字符串,因为用户在输入该字符时会遇到一些困难! But depending on your application and string processing this could cause damage, too, as there are some issues concerning incompatibilities (eg xml 1.0 doesn't support it at all ). 但是根据您的应用程序和字符串处理,这也可能导致损坏,因为存在一些与不兼容性有关的问题(例如xml 1.0根本不支持它 )。

Otherwise if only one or none of the substrings may contain such a character you can easily use one of the already presented methods to split up the string and safely extract the data. 否则,如果子字符串中只有一个或不包含这样的字符,则可以轻松地使用已经介绍的方法之一来拆分字符串并安全地提取数据。

This will work only if User/password doesn't contain @ or ; 仅当用户/密码不包含@;

    String loginStr = "login@User;Password";

    String []splittedLogin = loginStr.split("@");

    String []loginCredentials = splittedLogin[1].split(";");

    String user = loginCredentials[0];
    String password = loginCredentials[1];

    System.out.println(user);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM