Java regex - combining expressions -
i trying write code returns credit card vendor using regular expression, seem work, eg:
// visa - begins digit 4, 13 or 16 digits long ^[4].{12}|^[4].{15} // visa electron - begin code pattern, 16 digits long 4026.{12}|417500.{10}|4405.{12}|4508.{12}|4844.{12}|4913.{12}|4917.{12} so method isvisa want regex "return visa based on visa regex, not visa electron"
this code not work:
public static string isvisa(string number) { pattern p = pattern.compile("^[4].{12}|^[4].{15}&&(!4026.{12}|!417500.{10}|!4405.{12}|!4508.{12}|!4844.{12}|!4913.{12}|!4917.{12})"); matcher m = p.matcher(number); if (m.matches()) homecoming "visa"; else homecoming ""; }
the matches() method validates regular look against entire string, not need start ^ , end $ anchors. regular look engine matching these characters &! literals, seems trying utilize them operators.
to ignore patterns can utilize negative lookahead accomplish this.
(?!.*(?:(?:4026|4405|4508|4844|4913|4917).{12}|417500.{10}))(?=4.{12}|4.{15}).* example: returns true first 2 since rest not valid case.
string[] numbers = { "4000236512341234", "4000222213232222", "4026122222222222", "4175000000000000", "4405343434344343", "4508111111111111", "4844000000000000", "4913000000000000", "4917000000000000" }; pattern p = pattern.compile("(?!.*(?:(?:4026|4405|4508|4844|4913|4917).{12}|417500.{10}))(?=4.{12}|4.{15}).*"); (string x: numbers) { matcher m = p.matcher(x); system.out.println(m.matches()); } output:
true true false false false false false false false java regex
No comments:
Post a Comment