简体   繁体   中英

String pattern, regular expression

I would like to learn something about using string patterns .

For an email I have that

^[a-zA-Z0-9_]+@[a-zA-Z0-9]+\\.[a-z]+

It should be ok, or something to make better? I dont want anything complicated, just basic "friendly readable". Is there any way how to limit email domain for 2-3 chars via expression?

And for URL I've started with something like that

^(http|https)://[a-zA-Z0-9]+\\.[a-z]+(/[a-zA-Z0-9-_]+)+\\.[a-z_-]+

How to extend patter for URL parameters? Like:

http://hostname.domain/uriFrag1/uriFrag2/someFilename.fileExt?param1=val1&param2=val2

Any ideas how to simply make pattern for ?param1=val1&param2=val2 ?

There can be parameters, but they aren't mandatory.

^[a-zA-Z0-9_]+@[a-zA-Z0-9]+\\.[a-z]+

A few things about this. You should probably also add . and maybe even +, since both are quite common in emails (eg Gmail allows both). Secondly you can limit the number of characters with {from-to}. So:

^[a-zA-Z0-9_]+@[a-zA-Z0-9]+\\.[a-z]{1,3}

However, you might want to consider not doing this since longer domain names has just been sold. People might show up with .cloud or .email emails, and you wouldn't want to turn them away.

As for the url:

?param1=val1 this can easily be matched with

(\\?[a-z]*=[a-z]*)*

However, if you actually want to get them you might want to use some capture groups.

(\\?([a-z]*)=([a-z]*))*

There are many ways of doing that, I find split the simplest one. Since all parameters are passed after the question mark, you can simply:

String params = url.split("\\?")[1];

It's highly recommended to check the boundaries of the resulted array before trying to access the elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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