> ## Documentation Index
> Fetch the complete documentation index at: https://axiom.co/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# trim_start_regex

> This page explains how to use the trim_start_regex function in APL.

The `trim_start_regex` function removes all leading matches of a regular expression pattern from a string. Use this function to remove complex patterns from string beginnings, clean structured log prefixes, or normalize data with pattern-based trimming.

## For users of other query languages

If you come from other query languages, this section explains how to adjust your existing queries to achieve the same results in APL.

<AccordionGroup>
  <Accordion title="Splunk SPL users">
    In Splunk SPL, you use `rex` with mode=sed for pattern-based trimming. APL's `trim_start_regex` provides a more direct approach.

    <CodeGroup>
      ```sql Splunk example theme={null}
      | rex field=field mode=sed "s/^pattern//g"
      ```

      ```kusto APL equivalent theme={null}
      ['sample-http-logs']
      | extend cleaned = trim_start_regex('pattern', field)
      ```
    </CodeGroup>
  </Accordion>

  <Accordion title="ANSI SQL users">
    In ANSI SQL, regex-based trimming requires database-specific functions. APL's `trim_start_regex` provides standardized pattern-based trimming.

    <CodeGroup>
      ```sql SQL example theme={null}
      SELECT REGEXP_REPLACE(field, '^pattern', '') AS cleaned FROM logs;
      ```

      ```kusto APL equivalent theme={null}
      ['sample-http-logs']
      | extend cleaned = trim_start_regex('pattern', field)
      ```
    </CodeGroup>
  </Accordion>
</AccordionGroup>

## Usage

### Syntax

```kusto theme={null}
trim_start_regex(regex, text)
```

### Parameters

| Name  | Type   | Required | Description                                                  |
| ----- | ------ | -------- | ------------------------------------------------------------ |
| regex | string | Yes      | The regular expression pattern to remove from the beginning. |
| text  | string | Yes      | The source string to trim.                                   |

### Returns

Returns the source string with leading regex matches removed.

## Use case examples

<Tabs>
  <Tab title="Log analysis">
    Remove protocol and host prefixes from full URLs to extract paths.

    **Query**

    ```kusto theme={null}
    ['sample-http-logs']
    | extend full_url = strcat('https://api.example.com', uri)
    | extend path_only = trim_start_regex('https?://[^/]+', full_url)
    | summarize request_count = count() by path_only, method
    | sort by request_count desc
    | limit 10
    ```

    [Run in Playground](https://play.axiom.co/axiom-play-qf1k/query?initForm=%7B%22apl%22%3A%22%5B%27sample-http-logs%27%5D%20%7C%20extend%20full_url%20%3D%20strcat\(%27https%3A%2F%2Fapi.example.com%27%2C%20uri\)%20%7C%20extend%20path_only%20%3D%20trim_start_regex\(%27https%3F%3A%2F%2F%5B%5E%2F%5D%2B%27%2C%20full_url\)%20%7C%20summarize%20request_count%20%3D%20count\(\)%20by%20path_only%2C%20method%20%7C%20sort%20by%20request_count%20desc%20%7C%20limit%2010%22%7D)

    **Output**

    | path\_only  | method | request\_count |
    | ----------- | ------ | -------------- |
    | /api/users  | GET    | 2341           |
    | /api/orders | POST   | 1987           |

    This query strips protocol and host information from URLs to focus on path-based analysis.
  </Tab>

  <Tab title="OpenTelemetry traces">
    Remove environment and instance prefixes from service names using regex.

    **Query**

    ```kusto theme={null}
    ['otel-demo-traces']
    | extend cleaned_service = trim_start_regex('^(prod|dev|staging)-[0-9]+-', ['service.name'])
    | summarize span_count = count() by cleaned_service
    | sort by span_count desc
    | limit 10
    ```

    [Run in Playground](https://play.axiom.co/axiom-play-qf1k/query?initForm=%7B%22apl%22%3A%22%5B%27otel-demo-traces%27%5D%20%7C%20extend%20cleaned_service%20%3D%20trim_start_regex\(%27%5E\(prod%7Cdev%7Cstaging\)-%5B0-9%5D%2B-%27%2C%20%5B%27service.name%27%5D\)%20%7C%20summarize%20span_count%20%3D%20count\(\)%20by%20cleaned_service%20%7C%20sort%20by%20span_count%20desc%20%7C%20limit%2010%22%7D)

    **Output**

    | cleaned\_service | span\_count |
    | ---------------- | ----------- |
    | frontend         | 4532        |
    | checkout         | 3421        |
    | cart             | 2987        |

    This query removes environment names and instance numbers from the beginning of service names, enabling service-level aggregation across all deployments.
  </Tab>

  <Tab title="Security logs">
    Remove timestamp or log level prefixes from security event messages.

    **Query**

    ```kusto theme={null}
    ['sample-http-logs']
    | extend simulated_message = strcat('ERROR: ', uri)
    | extend cleaned_message = trim_start_regex('^(ERROR|WARN|INFO): ', simulated_message)
    | project _time, simulated_message, cleaned_message, id, status
    | limit 10
    ```

    [Run in Playground](https://play.axiom.co/axiom-play-qf1k/query?initForm=%7B%22apl%22%3A%22%5B'sample-http-logs'%5D%20%7C%20extend%20simulated_message%20%3D%20strcat\('ERROR%3A%20'%2C%20uri\)%20%7C%20extend%20cleaned_message%20%3D%20trim_start_regex\('%5E\(ERROR%7CWARN%7CINFO\)%3A%20'%2C%20simulated_message\)%20%7C%20project%20_time%2C%20simulated_message%2C%20cleaned_message%2C%20id%2C%20status%20%7C%20limit%2010%22%7D)

    **Output**

    | \_time               | simulated\_message | cleaned\_message | id      | status |
    | -------------------- | ------------------ | ---------------- | ------- | ------ |
    | 2024-11-06T10:00:00Z | ERROR: /admin      | /admin           | user123 | 403    |

    This query removes log level prefixes from security messages, extracting the core message content for analysis.
  </Tab>
</Tabs>

## List of related functions

* [trim\_start](/apl/scalar-functions/string-functions/trim-start): Removes leading characters. Use this for simple character-based trimming without regex.
* [trim\_regex](/apl/scalar-functions/string-functions/trim-regex): Removes both leading and trailing regex matches. Use this for bidirectional pattern trimming.
* [replace\_regex](/apl/scalar-functions/string-functions/replace-regex): Replaces regex matches. Use this when you need to replace patterns rather than just remove leading ones.
* [trim\_end\_regex](/apl/scalar-functions/string-functions/trim-end-regex): Removes trailing regex matches. Use this to trim patterns from the end.
