質問

I wish to detect all the combination of alpha and number together in string.

For example,

http://www.suleymanmemnun.com/~zeedee/9a70c0acdb2584924f5d0/web.php?#//uk/cgi-bin/confirm.php?cmd=login-submit&dispatch=0ec5d97d42ff8ff75e46c02a034bdf3bb4bf53c0db18fd85h0a134

in the above url, i want to split into part by '/', then detect whether that part contain the combination of number and alpha. if yes then 1, else 0

www.suleymanmemnun.com--->0
~zeedee---->0
9a70c0acdb2584924f5d0---->1
web.php?#----->0
役に立ちましたか?

解決

MATLAB has an in-built function isstrprop to detect parts of a string that are either alphabet or numeric digit. The syntax is:

A = isstrprop(input_string, 'alphanum')

This is used here on each part of the split URL string.

Code

%//url_string is the given URL string

s1 = regexp(url_string,'/','Split');
out = any(cellfun(@all,isstrprop(s1(~cellfun(@isempty,s1)),'alphanum')))

Explanation

To understand the code, let's break it down into parts -

%// Input URL string
url_string = 'http://www.suleymanmemnun.com/~zeedee/9a70c0acdb2584924f5d0/web.php?#//uk/cgi-bin/confirm.php?cmd=login-submit&dispatch=0ec5d97d42ff8ff75e46c02a034bdf3bb4bf53c0db18fd85h0a134'

%// Split into parts based on '/'
split1 = regexp(url_string,'/','Split');

%// Remove the empty split parts 
out_parts = split1(~cellfun(@isempty,split1))

We will get -

out_parts = 

    'http:'    'www.suleymanmemnun.com'    '~zeedee'    '9a70c0acdb2584924f5d0'    'web.php?#'    'uk'    'cgi-bin'

    'confirm.php?cmd=login-submit&dispatch=0ec5d97d42ff8ff75e46c02a034bdf3bb4bf53c0db18fd85h0a134'

Next, we use isstrprop on each of the URL parts -

out_detects = cellfun(@all,isstrprop(out_parts,'alphanum'))

It gives -

out_detects =

     0     0     0     1     0     1     0     0

Finally, since you are looking to find any such combination, we would use any command to get the final output -

out = any(out_detects)

The final answer is 1.

Multiple URL strings case

If you have a file that contains all the URL strings that you would like to test out for the mentioned title of the question, use this -

%// url_strings_filepath is the file that contains all the URLs line by line 
url_string_array = importdata(url_strings_filepath);

out = false(1,numel(url_string_array));
for k = 1:numel(url_string_array)
    s1 = regexp(char(url_string_array(k)),'/','Split');
    out(k) = any(cellfun(@all,isstrprop(s1(~cellfun(@isempty,s1)),'alphanum')));
end
disp(out)

他のヒント

Assuming you want to detect parts that contain one or more letters, one or more numbers, and possibly other characters too:

parts = regexp(str,'/+','split'); %// divide strings into parts
result = cellfun(@(p) ~isempty(regexp(p,'[a-z_A-Z]')) & ~isempty(regexp(p,'\d')), parts);

With your example URL this gives:

parts = 
    http:
    www.suleymanmemnun.com
    ~zeedee
    9a70c0acdb2584924f5d0
    web.php?#
    uk
    cgi-bin
    confirm.php?cmd=login-submit&dispatch=0ec5d97d42ff8ff75e46c02a034bdf3bb4bf53c0db18fd85h0a134

result =
     0     0     0     1     0     0     0     1

To determine if at least one of the parts of the string contains what you want: use any(result). In the example that would be 1.


Assuming you want to detect parts that contain any combination of letters and numbers, and no other characters:

parts = regexp(str,'/+','split'); %// divide strings into parts
result = cellfun(@(p) numel(regexp(p,'[a-z_A-Z_0-9]'))==numel(p) , parts)

In your example:

result =
    0     0     0     1     0     1     0     0
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top