5시간코딩
문자 본문
oracle(select, from 생략) | r + (::stringr) | python(::re) | |
대문자로 | upper(last_name) |
to_upper('asd') str_to_upper('abc') |
'abc'.upper() |
소문자로 | lower(last_name) |
to_lower('ASD') str_to_lower('abc') |
'ABC'.lower() |
첫글자대문자+소문자 | initcap(last_name) | str_to_title('abc') |
'abc'.capitalize() 'ab cd'.title() |
'abCD'.swapcase() | |||
문자 | letter[1:26] |
조회
문자갯수(char/bite) |
length(last_name) lengthb(last_name) |
nchar('qwe',type='char') nchar('가나다',type='bytes') str_length('가나다') |
len('abcd') |
특정 문자의 위치 |
instr(last_name,'a',1,2) |
grep('a',c('a','ba','baac','bad')) grep('a',c('a','ba','baac','bad'),value=T) str_locate('january','a') str_locate_all('january','a') |
for match in re.finditer('인공지능',text): print("{} : {}".format(match.start(),match.end())) |
str_view(text,'s') | text.count('인공지능') | ||
str_detect(text,'SQL') |
bool(re.match('c+a','ccat')) bool(re.match('c+a','ccat',re.I)) bool(re.search('a','ccat')) |
||
str_count(text,'s') | |||
str_extract(text, 'R') str_extract_all(text,'R') |
re.findall('a', 'Data Science') |
자르기, 합치기, 수정, 제거
두 문자 합침 | concat(first_name,last_name) |
paste0('abc','abc') str_c('aaa','bbb') str_c('aaa','bbb',sep=' ') str_c(c('aaa','bbb'),collapse='') |
' '.join('abcd') ' '.join(['ab','cd','ef']) |
문자 쪼갬 |
strsplit('ab.cd.ef','\\.') |
'abcd'.split(' ') re.split(':','python:programming') |
|
지정한 위치만큼 출력 |
substr(last_name,1,3) substrb(last_name,1,6) |
substr('abcabc',1,4) | |
대체 | replace(salary,0,'*') |
sub('a','z','abcabc') str_replace('banana','a','*') str_replace_all('banana','a','*') |
source.replace('Science','Scientist') re.sub('Science','Scientist',source) |
맨처음/끝 문자/공백 삭제 |
trim('a' from 'aabcaa') ltrim('aabcaa','a') rtrim(' bc ',' ') |
x='helloh' x.strip('h') x.lstrip('h') x.rstrip('h') |
기타
lpad(salary, 10,'*') rpad(salary, 10, '*') |
'abc'.center(20)
'abc'.ljust(20) 'abc'.rjust(20) |
||
str_sort(c(1,4,7,10)) str_sort(c(1,4,7,10),numeric=TRUE) |
|||
str_dup('파도',10) | |||
\g<1> \g<2> | |||
p = re.compile('\w+') txt = "Let's live happliy" p.findall(txt) |
정규표현식
* + ? | * + ? | ||
^ $ \\b | |||
{n} {n,} {n,m} | {n} {n,} {n,m} | ||
. [*|$|^] [0-9] [a-z] [A-Z] [c,d,e] [c-e] [c-e] \\^ \\& [^c] [^c-e] | |||
[[:digit:]] [[:upper:]] [[:lower:]] [[:alpha:]] [[:alnum:]] [[:punct:]] [[:space:]] |
[a-zA-Z] [가-힣] [0-9] \d [0-9] \D [^0-9] \s \S \w [a-zA-z0-9] \W |
1. 문자함수
1.1.
upper, lower, initcap
select * from employees where last_name = initcap(:v_id);
-->기본값을 바꾸지 말고, 비교값을 바꾸는건 괜찮음.
1.2.
concat
select last_name||first_name, concat(last_name, first_name) from employees;
1.3.
length, lengthb
select length(last_name) from employees;
select length('홍길동') from dual;
select * from length('빅데이터'), lengthb('빅데이터'), lengthb('bigdata') from dual;
->데이터베이스의 캐릭터셋에 영향을 받으므로 한글을 3바이트로 인식함.
1.4.
instr
select instr(last_name, 'a') from employees;
select instr('abcabc', 'a') from dual; -------->처음a가 있는 위치 값을 출력
select instr('abcabc', 'a', 1, 2) from dual; --->1은 1번위치부터 스캔한다는 뜻. 2는 두번째 'a'를 찾는다는 뜻.
1.5.
substr, substrb
select substr(last_name, 1, 2), -->처음부터 2개 추출
substr(last_name,-2,2) --------->뒤에서부터 2개추출
from employees;
select substr('abcde',1,1) ---->1바이트째부터 1바이트만큼 추출
substr('가나다라마',4,3) ----->4바이트째부터 3바이트만큼 추출. database 캐릭터셋이 AL32UTF8이라 한글을 3바이트씩 인식함.
from dual;
1.6.
trim, ltrim, rtrim
select trim('b' from 'baabbccbb') from dual; --> 결과값이 aabbcc 임
select ltrim('baabbccbb', 'b') from dual; --> trim하고 형식 다르니 주의
select ltrim('baabbccbb', 'b') from dual;
1.7.
replace
select replace('100-001', '-', '%') from dual;
1.8.
lpad, rpad
select lpad(salary, 10, '*'),
rpad(salary, 10, '*')
from employees;