사용하는 경우:
- 유효성 검사(Validation): 문자열이 특정 조건을 충족시키는지 검증할 수 있다. 예를 들어 이메일 주소가 올바른 형식인지 확인.
- 검색 후 바꾸기(Search and Replace): 특정 패턴을 가진 문자열을 찾아내고 그 부분을 수정해 줄 수 있다.원하지 않는 문자를 지우거나 포맷을 바꾸는데 유용하다.
- 택스트 분석(Text Parsing): 특정 패턴을 가진 문자열을 모아올 수 있다.
- 데이터 클리닝(Data Cleaning): 앞뒤로 비어있는 데이터의 빈공간을 지워주거나 텍스트를 소문자 대문자로 바꾸어주거나 중복되는 부분 제거 가능.
Regular expressions are a powerful tool that can save a lot of time and effort when working with text data. They are commonly used in many programming languages and in many different types of applications, including text editors, web development, and data analysis.
1. Digits
- 정수 – /^\d+$/
- 소수 – /^\d*\.\d+$/
- 정수+ 소수 – /^\d*(\.\d+)?$/
- 양수+음수+소수 – /^-?\d*(\.\d+)?$/
- 정수+소수+분수 – /[-]?[0-9]+[,.]?[0-9]*([\/][0-9]+[,.]?[0-9]*)*/
2. Alphanumeric Characters
- Alphanumeric without space – /^[a-zA-Z0-9]*$/
- Alphanumeric with space – /^[a-zA-Z0-9 ]*$/
alphanumeric 캐릭터는 영문+숫자+몇몇의 특수문자를 포함한다.
3. Email
- Common email Ids – /^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})*$/
- Uncommon email ids – /^([a-z0-9_\.\+-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
4. Password Strength
- Complex: 최소 영소문자/영대문자/숫자/특수문자 포함해야 하고 최소 8글자여야함.
/(?=(.*[0-9]))(?=.*[\!@#$%^&*()\\[\]{}\-_+=~`|:;"'<>,./?])(?=.*[a-z])(?=(.*[A-Z]))(?=(.*)).{8,}/
- Moderate: 최소 영소문자/영대문자/숫자 포함해야 하고 최소 8글자여야함.
/(?=(.*[0-9]))((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.{8,}$/
5. Username
- Alphanumeric string that may include _ and – having a length of 3 to 16 characters –
/^[a-z0-9_-]{3,16}$/
6. URL
- Include http(s) Protocol
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#()?&//=]*)/
- Protocol Optional
/(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/
7. IP Address
1
2
3
4
5
6
|
/* Match IPv4 address */
/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/
/* Match IPv6 address */
/(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/
/* Match both IPv4, IPv6 addresses */
/((^\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0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/
|
8. Dates
- Date Format YYYY-MM-dd using separator -
- Date Format dd-MM-YYYY using separators - or . or /
- Date Format dd-mmm-YYYY using separators - or . or /
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/* Date Format YYYY-MM-dd */
/([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/
/* Date Format dd-MM-YYYY or
dd.MM.YYYY or
dd/MM/YYYY
with check for leap year */
/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/
/* Date Format dd-mmm-YYYY or
dd/mmm/YYYY or
dd.mmm.YYYY */
/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/
|
9. Time
- Time Format HH:MM 12-hour, optional leading 0
/^(0?[1-9]|1[0-2]):[0-5][0-9]$/ - Time Format HH:MM 12-hour, optional leading 0, Meridiems (AM/PM)
/((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))/ - Time Format HH:MM 24-hour with leading 0
/^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/ - Time Format HH:MM 24-hour, optional leading 0
/^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/ - Time Format HH:MM:SS 24-hour
/(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)/
10. HTML Tags
- Elements with Attributes /<\/?[\w\s]*>|<.+[\W]>/
11. Javascript Handlers
- Inline JS handler /\bon\w+=\S+(?=.*>)/
- Inline JS handler with element /(?:<[^>]+\s)(on\S+)=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/
12. Slug
- Slug /^[a-z0-9]+(?:-[a-z0-9]+)*$/
13. Match Duplicates in a String
- Search Duplicates /(\b\w+\b)(?=.*\b\1\b)/
14. Phone Numbers
- International Phone Numbers – with optional country code/extension
1
2
|
/* International Phone Numbers */
/^(?:(?:\(?(?:00|\+)([1-4]\d\d|[1-9]\d?)\)?)?[\-\.\ \\\/]?)?((?:\(?\d{1,}\)?[\-\.\ \\\/]?){0,})(?:[\-\.\ \\\/]?(?:#|ext\.?|extension|x)[\-\.\ \\\/]?(\d+))?$/
|
Note: Use regex for validating phone numbers only if you don’t have the choice to use a library. There are several libraries that handle phone numbers more accurately and should be used instead.
15. File Path
- File Path with Filename and extension
- File Path with optional Filename, extension
- File Name with extension having 3 chars
'Android (Kotlin) > kotlin 문법' 카테고리의 다른 글
Activity 활동 수명 주기 (1) | 2023.12.22 |
---|---|
[Android Kotlin] Focusable vs Clickable vs Enable (0) | 2023.12.20 |
EditText Input Type 모두 모음 (0) | 2023.12.19 |
동적 메모리 할당 (Dynamic Allocation) | 스택(Stack) vs 힙(Heap) (0) | 2023.12.12 |
[Kotlin] Map mapOf(), mutableMapOf(), hashMapOf(), sortedMapOf(), linkedMapOf() (0) | 2023.12.06 |