public:computer:regexp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
public:computer:regexp [2021/08/23 21:34] alexpublic:computer:regexp [2023/01/01 00:58] (current) – [선택, 그룹, 역참조] alex
Line 1: Line 1:
 ====== Regular Expressions ====== ====== Regular Expressions ======
-<lead>정규표현식은 특정 문자열을 가리키는 패턴이다. 즉 이 패턴으로 원하는 문자열을 찾는다.</lead> - 켄 톰슨+**정규표현식은 특정 문자열을 가리키는 패턴이다. 즉 이 패턴으로 원하는 문자열을 찾는다.** - 켄 톰슨
  
 ===== 기본 패턴 찾기 ===== ===== 기본 패턴 찾기 =====
Line 14: Line 14:
 ^ .  | 점  | U+002E  | 임의의 문자를 찾음  | ^ .  | 점  | U+002E  | 임의의 문자를 찾음  |
 ^ \  | 역슬래시  | U+005C  | 문자의 특수 의미를 없앰  | ^ \  | 역슬래시  | U+005C  | 문자의 특수 의미를 없앰  |
-^ |  | 세로줄  | U+007C  | 선택 +%%|%%  | 세로줄  | U+007C  | 선택 
-^ ^  | 악센트 기호  | U+005E  | 행의 시작을 나타내는 앵커  |+%%^%%  | 악센트 기호  | U+005E  | 행의 시작을 나타내는 앵커  |
 ^ $  | 달러 기호  | U+0024  | 행의 끝을 나타내는 앵커  | ^ $  | 달러 기호  | U+0024  | 행의 끝을 나타내는 앵커  |
 ^ ?  | 물음표  | U+003F  | 0번 또는 한 번을 나타내는 수량자  | ^ ?  | 물음표  | U+003F  | 0번 또는 한 번을 나타내는 수량자  |
Line 70: Line 70:
  
 ==== 참조 그룹 ==== ==== 참조 그룹 ====
-  * 찾고자 하는 내용을 ()로 감싼 후(참조) $1, $2 혹은 \1, \2와 같이 역참조+  * 찾고자 하는 내용을 ()로 감싼 후(참조) \$1, \$2 혹은 \1, \2와 같이 역참조
  
  
Line 126: Line 126:
  
   * 서브패턴 ex) (the|The|THE), (t|T)h(e|eir)   * 서브패턴 ex) (the|The|THE), (t|T)h(e|eir)
-  * 그룹 참조와 역참조; \1, $1 +  * 그룹 참조와 역참조; \1, \$1 
   * 그룹 이름 지정; ?<one>, ?<two>   * 그룹 이름 지정; ?<one>, ?<two>
-  * 그룹 이름으로 참조; $+{one}, $+{two}+  * 그룹 이름으로 참조; \$+{one}, \$+{two}
  
 ^  그룹 이름 지정 구문  ^^ ^  그룹 이름 지정 구문  ^^
Line 254: Line 254:
     - -가 2개 이상     - -가 2개 이상
     - >로 닫음      - >로 닫음 
 +
 +  * <del>이름 masking; <alert type="info"><nowiki>(JAVA) str.replaceFirst("(\\W)\\W+\$", "\$1*****"))</nowiki></alert></del>
 +  * 숫자를 제외한 모든 문자 제거; <alert type="info"><nowiki>(JAVA) strConverted = str.replaceAll("\\D", "");</nowiki></alert>
 +  * 숫자만으로 되어있는 사업자 번호 -> 사업자번호 형식; <alert type="info"><nowiki>(JAVA) strConverted = str.replaceFirst("(^\\d{3})(\\d{2})(\\d{5})\$", "\$1-\$2-\$3");</nowiki></alert>
 +  * 숫자만으로만 되어있는지 확인; <alert type="info"><nowiki>(JAVA) bDigitsOnly = str.matches("^\\d+\$");</nowiki></alert>
 +  * 첫글자만 보이게 마스킹; <alert type="info"><nowiki>(JAVA) strMasked = str.replaceFirst("(^\\S)\\S+\$", "\$1*****");</nowiki></alert>
 +  * Password Validataions (JAVA & TypeScript)<sxh java title:Password Validation for JAVA>
 +
 +    public static Boolean hasSpecialCharacter(String strPassword) 
 +    {
 +        Boolean bHasSpecialCharacter = Pattern.matches("^(?=.*[\\x21-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\x7e]).{1,}", strPassword);
 +
 +        if(true == bHasSpecialCharacter)
 +        {
 +            System.out.println(strPassword + " has Special Character.");
 +        }
 +        else
 +        {
 +            System.out.println(strPassword + " has no Special Character.");
 +        }
 +
 +        return bHasSpecialCharacter;
 +    }
 +
 +    public static Boolean isValidPassword(String strPassword) 
 +    {
 +        Boolean bValid = Pattern.matches("^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\\x21-\\x2f|\\x3a-\\x40|\\x5b-\\x60|\\x7b-\\x7e]).{10,16}", strPassword);
 +
 +        System.out.println(strPassword + ":" + bValid);
 +        return bValid;
 +    }
 +
 +    public static Boolean hasAlphabet(String strPassword) 
 +    {
 +        Boolean bHasAlphabet = Pattern.matches("^(?=.*[a-zA-Z]).{1,}", strPassword);
 +
 +        if(true == bHasAlphabet) 
 +        {
 +            System.out.println(strPassword + " has Alphabet.");
 +        }
 +        else
 +        {
 +            System.out.println(strPassword + " has no Alphabet.");
 +        }
 +
 +        return bHasAlphabet;
 +    }
 +
 +    public static Boolean hasDigit(String strPassword)
 +    {
 +        Boolean bHasDigit = Pattern.matches("^(?=.*[\\d]).{1,}", strPassword);
 +
 +        if(true == bHasDigit)
 +        {
 +            System.out.println(strPassword + " has Digits.");
 +        }
 +        else
 +        {
 +            System.out.println(strPassword + " has no Digits.");
 +        }
 +
 +        return bHasDigit;
 +    }
 +
 +    public static void testPassword(String strPassword)
 +    {
 +        System.out.println("Your password is : " + strPassword);
 +
 +        hasAlphabet(strPassword);
 +        hasDigit(strPassword);
 +        hasSpecialCharacter(strPassword);
 +    }</sxh><sxh typescript title:Password Validation for TypeScript>
 +function hasAlphabet(strPassword: string) {
 +  const reg = new RegExp("^(?=.*[a-zA-Z]).{1,}$")
 +  const bHasAlphabet = reg.test(strPassword)
 +
 +  if(false === bHasAlphabet) {
 +    console.log(`${strPassword} has no alphabet.`)
 +  } else {
 +    console.log(`${strPassword} has alphabet.`)
 +  }
 +    
 +  return bHasAlphabet
 +}
 +
 +function hasDigit(strPassword: string) {
 +  const reg = new RegExp("^(?=.*[0-9]).{1,}$")
 +  const bHasDigit = reg.test(strPassword)
 +
 +  if(false === bHasDigit) {
 +    console.log(`${strPassword} has no digit.`)
 +  } else {
 +    console.log(`${strPassword} has digit.`)
 +  }
 +
 +  return bHasDigit
 +}
 +
 +function hasSpecialCharacter(strPassword: string) {
 +  const reg = new RegExp("^(?=.*[\\x21-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\x7e]).{1,}$")
 +  const bHasSpecialCharacter = reg.test(strPassword)
 +
 +  if(false === bHasSpecialCharacter){
 +    console.log(`${strPassword} has no special character.`)
 +  } else {
 +    console.log(`${strPassword} has special character.`)
 +  }
 +
 +  return bHasSpecialCharacter
 +}
 +
 +function isValidPassword(strPassword: string) {
 +  const reg = new RegExp("^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[\\x21-\\x2f|\\x3a-\\x40|\\x5b-\\x60|\\x7b-\\x7e]).{10,16}$")
 +  const bValid = reg.test(strPassword)
 +
 +  if(false === bValid) {
 +    console.log(`${strPassword} is invalid.`)
 +  } else {
 +    console.log(`${strPassword} is valid.`)
 +  }
 +
 +  return bValid
 +}
 +
 +function testPassword(strPassword: string) {
 +  console.log(`Your Password is : ${strPassword}`)
 +
 +  hasAlphabet(strPassword)
 +  hasDigit(strPassword)
 +  hasSpecialCharacter(strPassword)
 +
 +  isValidPassword(strPassword)
 +
 +}</sxh>
 +  * camelCase to snake_case and MACRO_CASE (JAVA)<sxh java title:camelCase to snake_case and MACRO_CASE for JAVA>
 +    public static String getSnakeCaseFromCamelCase(String strCamelCase)
 +    {
 +        return strCamelCase.replaceAll("([A-Z])", "_$1").toLowerCase();
 +    }
 +
 +    public static String getMacroCaseFromCamelCase(String strCamelCase) 
 +    {
 +        return strCamelCase.replaceAll("([A-Z])", "_$1").toUpperCase();
 +    }
 +</sxh>
 +  * Card Number Validation (TypeScript)<sxh typescript title:Card Number Validation Example for TypeScript>
 +function validateIBKCEOCard(strCardNumber: string) {
 +  const regDigitsOnly = /(\D+)/gi
 +  const strDigits = strCardNumber.replace(regDigitsOnly, "")
 +
 +  if (strDigits.length != 16) {
 +    console.log(strCardNumber + ' is invalid card number')
 +    return
 +  }
 +  
 +  console.log('validate IBK CEO Card: ' + strCardNumber + ' => ' + strDigits + '(' + strDigits.length + ')')
 +
 +  const regValidate: RegExp = /^943003|^552103\d+$/g
 +  const bValid = regValidate.test(strDigits)
 +
 +  console.log('is valid? ' + bValid)
 +}
 +</sxh>
 +
  
  
Line 269: Line 433:
   * [[https://www.regexpal.com/|RegEx Pal]]   * [[https://www.regexpal.com/|RegEx Pal]]
   * [[https://regexr.com/|RegExr]]   * [[https://regexr.com/|RegExr]]
-  * rubular+  * [[https://rubular.com/|Rubular]]
  
  
Line 283: Line 447:
   * [[https://www.oxygenxml.com/|Oxygen XML Editor]]   * [[https://www.oxygenxml.com/|Oxygen XML Editor]]
   * reggy   * reggy
 +
 +
 +===== References =====
 +  * [[https://jamesdreaming.tistory.com/182|[ 자바 코딩 ] REGULAR EXPRESSION 정규표현식 - 숫자만 허용하기]]
 +  * [[https://coding-factory.tistory.com/529|[Java] 자바 정규 표현식 (Pattern, Matcher) 사용법 & 예제]]
 +  * [[https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Regular_Expressions|정규 표현식]]
 +  * [[https://apiclass.tistory.com/entry/%EC%8B%A4%EC%A0%9C-%EC%82%AC%EC%9A%A9%ED%95%98%EB%8A%94-%EC%A0%95%EA%B7%9C%EC%8B%9D-%ED%8C%A8%ED%84%B4|실제 사용하는 정규식 패턴]]
 +  * [[https://intro0517.tistory.com/135|자바스크립트 정규식 정리(숫자만, 한글만, 메일, 전화번호, 비밀번호)]]
 +  * [[https://develop-sense.tistory.com/62|[JAVA] 정규식을 이용한 마스킹(정규표현식 마스킹 처리)]]
 +  * [[https://leejiheg.tistory.com/entry/%EC%A0%95%EA%B7%9C-%ED%91%9C%ED%98%84%EC%8B%9D-%EA%B8%B0%EB%B3%B8|정규 표현식 기본]]
 +  * [[https://youngjinmo.github.io/2020/01/reg/|정규표현식(Reg)]]
 +  * [[https://minj0i.tistory.com/26|[JAVA] 이름 마스킹처리]]
 +  * [[https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html|Class Pattern]]
 +  * [[https://blog.naver.com/PostView.nhn?blogId=realuv&logNo=220699272999|[Javascript] 정규식을 이용한 특수문자, 한글 등 특정 문자 체크(제거)]]
 +
 +
 +
 +
 +
 +
  • public/computer/regexp.1629722095.txt.gz
  • Last modified: 2021/08/23 21:34
  • by alex