c# - Regular expression to parse FTP link string -


i have following code parse parts of ftp link:

regex exp = new regex(@"(?i)ftp:\/\/(?<user>\s+?):(?<passwd>\s+?)@(?<host>\s+?.\s+?.\s+?.\s+?)"); match m = exp.match(@"link: ftp://username:password@host.sub.domain.tld<ftp://username:password@host.sub.domain.tld/>");  console.writeline("host = " + m.groups["host"].value); console.writeline("user = " + m.groups["user"].value); console.writeline("pass = " + m.groups["passwd"].value); 

which produces following output:

host = host.su user = username pass = password 

why host being truncated?

because \s match dot character , . match character.

@"(?i)ftp:\/\/(?<user>\s+?):(?<passwd>\s+?)@(?<host>[^.\s]+\.[^.\s]+\.[^.\s]+\.\w+)" 

demo

why?

(?<host>\s+?.\s+?.\s+?.\s+?) 
  • \s+? - matches first charcter because of non-greediness.
  • . - matches second character, since unescaped dot match character.
  • likewise matches first 7 chars in host part.

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -