본문 바로가기
WIKI 기술 지식 베이스

substringAfterLast() function

원문 보기 위키 갱신

출처: 문서

본문

substringAfterLast() 함수

substringAfterLast() 함수는 문자열에서 지정한 구분자가 마지막으로 나타난 위치 뒤의 부분을 추출할 때 사용해요. 이 함수는 두 개의 인수를 받아요:

  1. 첫 번째 인수는 리터럴 문자열이거나 소스 문자열을 나타내는 JSON pointer예요.

  2. 두 번째 인수는 첫 번째 인수 안에서 찾을 구분자 문자열이에요.

구분자를 찾으면 함수는 구분자가 마지막으로 나타난 위치 뒤의 문자열 부분을 반환해요. 구분자를 찾지 못하면 원본 문자열을 그대로 반환해요. 소스가 null로 평가되면 함수는 null을 반환해요. 구분자가 null이거나 비어 있으면 원본 문자열을 반환해요.

예를 들어 파일 경로가 담긴 /filepath 필드에서 파일 확장자를 추출하려면 다음과 같이 substringAfterLast() 함수를 사용해요:

'substringAfterLast(/filepath, ".")'

{% include copy.html %}

/filepath 필드에 archive.tar.gz가 들어 있다면 함수는 gz를 반환해요.

또는 첫 번째 인수로 리터럴 문자열을 사용할 수도 있어요:

'substringAfterLast("one-two-three", "-")'

{% include copy.html %}

함수는 three를 반환하는데, 이는 마지막 - 문자 뒤의 문자열 부분을 추출하기 때문이에요.

substringAfterLast() 함수는 대소문자를 구분하는 검색을 수행해요. {: .note}

예제

다음 파이프라인은 substringAfterLast() 함수를 사용해 전체 파일 경로에서 파일 이름을 추출해요. 그리고 추출한 파일 이름을 filename이라는 새 필드로 추가해요:

substring-after-last-demo:
  source:
    http:
      ssl: false

  processor:
    - add_entries:
        entries:
          - key: filename
            value_expression: 'substringAfterLast(/filepath, "/")'

  sink:
    - opensearch:
        hosts: ["https://opensearch:9200"]
        insecure: true
        username: admin
        password: admin_password
        index_type: custom
        index: demo-index-%{yyyy.MM.dd}

{% include copy.html %}

다음 명령으로 파이프라인을 테스트할 수 있어요:

curl -sS -X POST "http://localhost:2021/log/ingest" \
  -H "Content-Type: application/json" \
  -d '[
        {"filepath":"/var/log/syslog"},
        {"filepath":"/home/user/docs/report.pdf"}
      ]'

{% include copy.html %}

OpenSearch에 저장된 문서에는 다음과 같은 정보가 들어 있어요:

{
  ...
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "demo-index-2026.03.13",
        "_id": "abc123",
        "_score": 1,
        "_source": {
          "filepath": "/var/log/syslog",
          "filename": "syslog"
        }
      },
      {
        "_index": "demo-index-2026.03.13",
        "_id": "def456",
        "_score": 1,
        "_source": {
          "filepath": "/home/user/docs/report.pdf",
          "filename": "report.pdf"
        }
      }
    ]
  }
}

더 알아보기 (Learn more)