项目作者: Jkutkut

项目描述 :
All regex expressions
高级语言:
项目地址: git://github.com/Jkutkut/Regex.git
创建时间: 2020-02-06T18:43:12Z
项目社区:https://github.com/Jkutkut/Regex

开源协议:

下载


Regex

All regex expressions made on diferent languages

Python 3:

Valid date:

  1. r'^(?:(?:(?:(?:(?:[13579][26]|[2468][048])00)|(?:[0-9]{2}(?:(?:[13579][26])|(?:[2468][048]|0[48]))))-(?:02-(?:0[1-9]|1[0-9]|2[0-8])))|(?:(?:[0-9]{4}-(?:(?:(?:0[13578]|1[02])-(?:0[1-9]|[12][0-9]|3[01]))|(?:(?:0[469]|11)-(?:0[1-9]|[12][0-9]|30))|(?:02-(?:0[1-9]|[1][0-9]|2[0-9]))))))$'

Detect Floating Point Number:

  1. r'^[+-]?\d*\.\d+$'

Valid Roman Numerals:

  1. r'M{0,3}(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[VX]|V?I{0,3})$'

Valid phone number:

  1. On this example, it must start with 7,8 or 9 and be length 9
  2. r'^[789]\d{8}$'


  1. Allow for prefix (+ sign and 2 or 3 digits) and 9 numbers (divided in grops of 3 or not)
  2. (\+\d{2,3})? ?\d{3} ?\d{3} ?\d{3}


  1. Spanish phone:
  2. ^(\+\d{2,3})? ?\d{3} ?(\d{3} ?\d{3}|\d{2} ?\d{2} ?\d{2})$

Valid Email Address:

  1. The username starts with an English alphabetical character, and any subsequent characters
  2. consist of one or more of the following: alphanumeric characters, -,., and _.
  3. The domain and extension contain only English alphabetical characters.
  4. The extension is max length 3, min length 1.
  5. r'^[a-zA-Z][a-zA-Z1-9._-]* <[a-z][a-z1-9._-]*@[a-z]+\.[a-z]{1,3}>$'
  6. user123 <user34_43@fjkds.com> it's correct
  7. if Email with no numbers:
  8. r'^([\w\.]+@\w+(\.\w+)+)$'
  9. Basic email:
  10. ^[a-z][a-z1-9._-]*@[a-z]+\.[a-z]{1,3}$

Valid Hex Color Code

  1. r'(?!^)(#[a-fA-F0-9]{6}|#[a-fA-F0-9]{3})'

Detect “&&” and “||” between spaces:

  1. r'(?<= )(&&|\|\|)(?= )'

Multiple test in order to check if valid password:

  1. import re;
  2. rex = [
  3. r'([A-Z]).*([A-Z])', #At least 2Upper english alphabet characters
  4. r'\d.*\d.*\d', #At least 3 numbers
  5. r'^[A-Za-z0-9]+$', #Only english alphabet and numbers
  6. r'^.{10}$', #Length = 10
  7. r'^(?:([A-Za-z0-9])(?!.*\1))*$' #No repetitions of characters
  8. ];
  9. case = input();
  10. print("Valid" if all([re.search(rex[j], case) for j in range(len(rex))]) else "Invalid");

Valid credit card:

  1. #These expressions can be used the same way as the "Multiple test in order to check if valid password".
  2. rex = [
  3. # start with 4, 5 or 6;
  4. # only numbers and total length = 16 digits
  5. # may be divided in 4-digits-group divided by "-"
  6. # do not repeat the same digit more than 3 times
  7. r'^[456]\d{3}(-?\d{4}){3}$',
  8. r'^(?:(\d)-?(?!(-?\1){3,}))+$'
  9. ];

JavaScript:

Correct Spannish IDs:

  1. ·Passport:
  2. This expression checks whenever a string named id is a passport:
  3. /^[A-Z][0-9]{8}$/
  4. ·CIF:
  5. whenever a string named id is a CIF
  6. /^([ABCDEFGHJKLMNPQRSUVW])(\d{7})([0-9A-J])$/
  7. ·NIE
  8. whenever a string named id is a NIE
  9. /^[XYZ]\d{7,8}[A-Z]$/

Java:

Website:

  1. ^https?://(www2?\.)?[-a-zA-Z\d@:%._\+~#=]{1,256}\.[a-zA-Z\d()]{1,6}\b([-a-zA-Z\d()@:%_\+.~#?&/=]*)$

Valid IP address:

  1. If IP v4:
  2. "^((([0-1]?[0-9]?[0-9]|[2][0-5][0-5])\\.){3}([0-1]?[0-9]?[0-9]|[2][0-5][0-5])|^([0-9a-f]{1,4}:){7}[0-9a-f]{1,4})$"
  3. Once it's valid, you can check if it is a IPV6:
  4. "^([0-9a-f]{1,4}:){7}[0-9a-f]{1,4}$"

Duplicated word on text:

  1. "\\b(\\w+)(?:\\W+\\1\\b)+"

Valid user name:

  1. The username consists of 8 to 30 characters inclusive. If the username
  2. consists of less than 8 or greater than 30 characters, then it is an
  3. invalid username.
  4. The username can only contain alphanumeric characters and underscores (_).
  5. Alphanumeric characters describe the character set consisting of lowercase
  6. characters [a-z], uppercase characters [A-Z], and digits [0-9].
  7. The first character of the username must be an alphabetic character.
  8. "[a-zA-Z][a-zA-Z_0-9]{7,29}"
  1. "(?<code1><a href=\".+?\")(?<code2>.*?[<h1><b>]*>[\\s]*)(?<code3>.*?<)"

Detect HTML Tags:

  1. "(?<=<)[a-z0-3]+"

Detect the Domain Name:

  1. "http[s]?:\\/\\/(www2?\\.)?(([a-zA-Z0-9\\-]+\\.)+([a-zA-Z\\-])+)"

Identifying comments:

  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import java.util.regex.*;
  6. public class Solution {
  7. public static void main(String[] args) {
  8. Scanner sc = new Scanner(System.in);
  9. String line = "";
  10. while(sc.hasNext()){
  11. line = sc.nextLine();
  12. Matcher m = Pattern.compile("\\/\\/.*$").matcher(line);
  13. if(m.find()){
  14. System.out.println(m.group());
  15. }
  16. else if(Pattern.compile("\\/\\*").matcher(line).find()){//start /*
  17. Matcher aux = Pattern.compile("\\/\\*").matcher(line);
  18. aux.find();
  19. String text = line.substring(aux.start(), line.length());
  20. while(!Pattern.compile("\\*\\/").matcher(line).find()){
  21. line = sc.nextLine();
  22. text += "\n"+line.trim();
  23. }
  24. System.out.println(text);
  25. }
  26. }
  27. }
  28. }

Detecting Valid Latitude and Longitude Pairs:

  1. "^\\([-+]?([1-8]?[0-9](\\.\\d+)?|90(\\.0+)?), [-+]?([1-9]?[0-9](\\.\\d+)?|1[0-7][0-9](\\.\\d+)?|180(\\.0+)?)\\)$"

Valid PAN format (Indian SSN):

  1. "[A-Z]{5}\\d{4}[A-Z]"

Programming Language Detection:

  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import java.util.regex.*;
  6. public class Solution {
  7. public static void main(String[] args) {
  8. Scanner sc = new Scanner(System.in);
  9. String line = "";
  10. while(sc.hasNext()){
  11. line = sc.nextLine();
  12. if(Pattern.compile("#include").matcher(line).find()){
  13. System.out.println("C");
  14. break;
  15. }
  16. else if(Pattern.compile("java").matcher(line).find()){
  17. System.out.println("Java");
  18. break;
  19. }
  20. else if(Pattern.compile("(print |def)").matcher(line).find()){
  21. System.out.println("Python");
  22. break;
  23. }
  24. }
  25. }
  26. }

Detect HTML Attributes

  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import java.util.regex.*;
  6. public class Solution {
  7. public static void main(String[] args) throws NumberFormatException, IOException {
  8. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  9. Pattern p = Pattern.compile("<(\\w+).*?(>|(\\/>))");
  10. Pattern pAttribute = Pattern.compile("\\s+(\\w+)=['\"].*?[\"']");
  11. int n = Integer.parseInt(br.readLine().trim());
  12. TreeMap<String, TreeSet<String>> map = new TreeMap<>();
  13. for (int i = 0; i < n; ++i) {
  14. String line = br.readLine().trim();
  15. Matcher m = p.matcher(line);
  16. while (m.find()) {
  17. String tag = m.group(1);//ex: div
  18. TreeSet<String> set = map.get(tag);
  19. if (set == null) {
  20. set = new TreeSet<>();
  21. map.put(tag, set);
  22. }
  23. String text = m.group();//ex: <a href="ab cd" attr1=' xyz'>
  24. Matcher m2 = pAttribute.matcher(text);
  25. while (m2.find()) {
  26. set.add(m2.group(1));
  27. }
  28. }
  29. }
  30. for(Map.Entry<String, TreeSet<String>> entry: map.entrySet()) {
  31. StringBuilder sb = new StringBuilder();
  32. sb.append(entry.getKey()).append(":");
  33. for(String attr: entry.getValue()) {
  34. sb.append(attr).append(",");
  35. }
  36. if(sb.charAt(sb.length() - 1) == ',') {
  37. sb.deleteCharAt(sb.length() - 1);
  38. }
  39. System.out.println(sb.toString());
  40. }
  41. }
  42. }

  1. /*
  2. given the numbers 1,2,3,4,5,6,7,8,9 on that order; find all combinations (with +-) that are equal to 100.
  3. eg: 1234+5678 != 100
  4. 12+34-567-89
  5. */
  6. public class reto {
  7. public static void main(String[] args) {
  8. int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  9. String[] symbol = {"","+","-"};
  10. /**
  11. Sistema ternario: 8 digits + 1
  12. 0 1 2 10 11 12 20 21 22 100
  13. max number = 8 digits --> 22222222 ==> 6561 possible combinations
  14. */
  15. Base ternario = new Base(3);//We work on base 3
  16. String resultado;
  17. String equation;
  18. for(int i = 0; i < 6561; i++){//6561
  19. ternario.setNumero(i);
  20. resultado = ternario.getResultado();
  21. equation = "1";
  22. for(int j = 0; j < 8; j++){
  23. equation = equation + symbol[Integer.parseInt(resultado.substring(j, j+1))] + numbers[j+1];
  24. }
  25. String[] parts = equation.split("(?=[/+])|(?<=[/+])|(?=[/-])|(?<=[/-])");
  26. double result = Double.parseDouble(parts[0]);
  27. for(int l = 1; l < parts.length; l+=2){
  28. switch (parts[l]) {//symbol
  29. case "+" :
  30. result += Double.parseDouble(parts[l+1]);
  31. break;
  32. case "-" :
  33. result -= Double.parseDouble(parts[l+1]);
  34. break;
  35. }
  36. }
  37. if(result == 100){
  38. System.out.println("Comb: " + equation);//sol
  39. }
  40. }
  41. }
  42. public static String operacion(String signo, int number1, int number2) {
  43. if(signo == ""){
  44. return Integer.toString(number1) + Integer.toString(number2);
  45. }
  46. else if(signo == "+"){
  47. return Integer.toString(number1 + number2);
  48. }
  49. else{
  50. return Integer.toString(number1 - number2);
  51. }
  52. }
  53. }