티스토리 뷰

dev

정규식 - Character Sets

altvirus 2015. 1. 30. 17:45

 Defining a character set

Matches single, lowercase vowels

Regex:  /[aeiou]/ : aeiou 중에 하나

String: "Bananas Peaches Apples"


Regex: /gr[ea]y/

String: "grey gray"


Regex: /gr[ea]t/

String: "great"


Regex: /gr[ea][ea]t/

String: "great graet greet graat"


□ Character ranges


Regex: /[ABCDEFGHIJKLMNOPQRSTUVWXYZ]ello/

String: "Hello"


Regex: /[A-Z]ello/ 

String: "Hello"


Regex: /[A-Za-z]/

String: "Hello"

- [a-ek-ou-y] 이런거도 한다



Phone number


String: "555-666-7890"

Regex: /[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]/

- [0-4] 또는 [2-7] 이런거도 한다.

- [1-36-9] : 1~3, 6~9의미.-> 4,5는 제외

- [50-90] 이런건 아님. 글자 하나씩만.


Postal codes


String: "90210"

Regex:  /[0-9][0-9][0-9][0-9][0-9]/


String: "WC2H 9AW"

Regex:  "[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z] [0-9A-Z][0-9A-Z][0-9A-Z]"


□ Negative character sets

^ 캐릭터 : ... 는 빼고.

Regex:  /[^a-z]/

String: "Now we know how to make negative character sets."



Negates the entire character set


Regex:  /[^a-zA-Z]/

String: "Now we know how to make negative character sets."



Only matches a single character


Regex:  /[^aeiou]/

String: "It seems I see the sea I seek."


Regex:  /see[^mn]/

String: "It seems I see the sea I seek."



Must match one character (which may be a space or punctuation)


Regex:  /see[^mn]/

String: "It seems I see the sea I see"


Regex:  /see[^mn]/

String: "It seems I see the sea I see."


□ Metacharacters inside character sets

- 이스케입 필요없다.

- 예외 : ] - ^ \

- /var[[(][0-9][\])]/

- 2003[-/]10[-/]05 : 필요없음

- /file[0-\_]1/ : 해야된다.


Regex:  /h[abc.xyz]t/

String: "hat hot h.t"


Regex:  /var[[(]\d[\])]/ 또는 /var[[(][0-9][\])]/

String: "var(3) var[4]"

- var + "[( 둘중하나" + 숫자 + ")]둘중하나"


May not require escaping

Regex:  /2003[-/]10[-/]05/

String: "2003/10/05 2003-10-05"


Regex:  /file[0-\_]1/

String: "file01 file-1 file\1 file_1"


□ Shorthand character sets




Regex:  /\d\d\d\d/

String: "1984 text"


Regex:  /\w\w\w\w/

String: "1984 text 1_5W"


Regex:  /[\w\-]/

String: "blue-green paint"


Regex:  /[\d\s]/

String: "123 456 789 abc"



Be careful when using negatives


Regex:  /[^\d\s]/

String: "123 456 789 abc"


Regex:  /[\D\S]/

String: "123 456 789 abc"


□ POSIX bracket expressions



- 혼자 쓰는거 아님. 

[[:alpha:]] 또는 [^[:alpha:]] (O)

[:alpha:] (X)


- 다른 약어와 섞어쓰지 말자.

- perl, PHP, Ruby, Unix에서는 되지만, Java, Javascript, .NET, Python에서는 안된다.


This command only works on Unix


ps aux | grep --regexp="s[[:digit:]]"



----

'dev' 카테고리의 다른 글

리소스  (1) 2015.04.21
파이썬 관련 강좌 올리는 님. django 등  (0) 2015.02.14
이클립스 moonrise 테마  (0) 2015.02.14
정규식 - Repetition Expressions  (0) 2015.02.02
정규식 - Characters  (0) 2015.01.30
공지사항