Question

I have some urls as below:

https://example.com/file/filegetrevision.do?fileEntityId=738007
9&cs=4Pzbb2jPu3EHBzv8RQHrGcPm4hZZkRC-CfH0my4dP0M.arv


https://example.com/file/filegetrevision.do?fileEntityId=&cs=2L
5cx4UsMsFJgM05pPtB_Z8dRdL4CXLLcTeDhGPDBIg.arv


https://example.com/file/filegetrevision.do?fileEntityId=2555874&cs=2L
5cx4UsMsFJgM05pPtB_Z8dRdL4CXLLcTeDhGPDBIg.arv

Now I need to check which url has numbers with fileEntityId or which has not? Any help in this regard? say first and third URL has 738007 and 2555874 numbers with fileEntityId but the second doesn't.

Thanks

Was it helpful?

Solution

As much as I love regexes, there are more appropriate tools included in the standard library:

require 'uri'
require 'cgi'

url = "https://example.com/file/filegetrevision.do?fileEntityId=7380079&cs=4Pzbb2jPu3EHBzv8RQHrGcPm4hZZkRC-CfH0my4dP0M.arv"
query = URI::parse(url).query # => "fileEntityId=7380079&cs=4Pzbb2jPu3EHBzv8RQHrGcPm4hZZkRC-CfH0my4dP0M.arv"
fileEntityId = CGI::parse(query)['fileEntityId'] # => ["7380079"]

Then you can check if it is a number or not.

OTHER TIPS

Here we use regexes to find out if a string contains "fileEntityId=" followed by one or more digits:

urls = ['https://example.com/file/filegetrevision.do?fileEntityId=7380079&cs=4Pzbb2jPu3EHBzv8RQHrGcPm4hZZkRC-CfH0my4dP0M.arv',
  'https://example.com/file/filegetrevision.do?fileEntityId=&cs=2L5cx4UsMsFJgM05pPtB_Z8dRdL4CXLLcTeDhGPDBIg.arv',
  'https://example.com/file/filegetrevision.do?fileEntityId=2555874&cs=2L5cx4UsMsFJgM05pPtB_Z8dRdL4CXLLcTeDhGPDBIg.arv']


urls.map {|u| !!(u =~ /fileEntityId=\d+/)} # => [true, false, true]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top