Develop/Linux
[정규표현식] 정규표현식 사용 예..
시크라기
2012. 4. 27. 10:06
1 #!/bin/sh 2 3 if [[ $# -lt 2 ]]; then 4 echo "Usage: $0 PATTERN STRINGS..." 5 exit 1 6 fi 7 regex=$1 8 shift 9 echo "regex: $regex" 10 echo $1 11 12 while [[ $1 ]] 13 do 14 if [[ $1 =~ $regex ]]; then 15 echo "$1 matches" 16 i=1 17 n=${#BASH_REMATCH[*]} 18 while [[ $i -lt $n ]] 19 do 20 echo " capture[$i]: ${BASH_REMATCH[$i]}" 21 let i++ 22 done 23 else 24 echo "$1 does not match" 25 fi 26 shift 27 done
이 shell script를 이용해 맞는지 체크해줄수 있음.
# [file_name].sh [expression] [string]
요렇게 실행..
^\#[0-9]+(\s|\\n)(fix|new|continue)
이 표현식은
#[숫자] [fix|new|continue] 이런 식이거나
#[숫자]
[fix|new|continue] 요런 식인 문장일때 만족함.