Regex Split API: Split Text Using Regular Expressions

Need to split text by complex patterns instead of simple delimiters? This guide shows you how to use the Regex Split API to tokenize text using regular expressions, handling multiple separators and advanced splitting scenarios.

What is Regex Split?

Regex split divides a string into an array of substrings using a regular expression pattern as the delimiter. Unlike simple string split which only handles fixed delimiters, regex split can match complex patterns like "one or more whitespace characters" or "comma optionally followed by space".

The pattern defines where to split; the text between matches becomes the array elements.

Regex Split vs Simple Split

Consider splitting "apple, banana,orange , grape":

  • Simple split by ",": ["apple", " banana", "orange ", " grape"] (inconsistent spacing)
  • Regex split by ",\s*": ["apple", "banana", "orange", "grape"] (clean results)
Tip: Use \s+ to split by one or more whitespace characters, or [,;] to split by multiple possible delimiters.

Using the Regex Split API

TinyFn provides a powerful endpoint for regex-based splitting:

API Request
POST https://api.tinyfn.io/v1/regex/split
Headers: X-API-Key: your-api-key
Content-Type: application/json
Request Body
{
  "text": "apple, banana;orange | grape",
  "pattern": "[,;|]\\s*"
}
Response
{
  "result": ["apple", "banana", "orange", "grape"],
  "original": "apple, banana;orange | grape",
  "parts_count": 4,
  "pattern": "[,;|]\\s*"
}

Parameters

Parameter Type Description
text string The input text to split (required)
pattern string Regular expression pattern to use as delimiter (required)
limit integer Maximum number of splits to perform (optional)
flags string Regex flags: i (case-insensitive), m (multiline)
remove_empty boolean Remove empty strings from result (default: false)

Code Examples

JavaScript / Node.js

const response = await fetch(
  'https://api.tinyfn.io/v1/regex/split',
  {
    method: 'POST',
    headers: {
      'X-API-Key': 'your-api-key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      text: 'one   two\tthree\nfour',
      pattern: '\\s+'  // Split by any whitespace
    })
  }
);
const result = await response.json();
console.log(result.result); // ["one", "two", "three", "four"]

Python

import requests

response = requests.post(
    'https://api.tinyfn.io/v1/regex/split',
    json={
        'text': 'Hello World! How are you? I am fine.',
        'pattern': r'[.!?]\s*'  # Split by sentence
    },
    headers={'X-API-Key': 'your-api-key'}
)
result = response.json()
print(result['result'])  # ["Hello World", "How are you", "I am fine", ""]

cURL

curl -X POST "https://api.tinyfn.io/v1/regex/split" \
  -H "X-API-Key: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"text":"a1b2c3d4","pattern":"[0-9]"}'

Common Use Cases

  • CSV Parsing: Split lines handling quoted values
  • Log Processing: Parse log entries with complex formats
  • Sentence Tokenization: Split text into sentences
  • Word Tokenization: Split text into words, ignoring punctuation
  • Data Import: Parse data with multiple delimiter types

Best Practices

  1. Handle empty strings: Trailing delimiters create empty strings; use remove_empty if needed
  2. Use limit wisely: Set a limit when you only need the first N parts
  3. Test edge cases: Check behavior with leading/trailing delimiters
  4. Consider capture groups: Parentheses in patterns may include delimiters in results

Use via MCP

Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:

{
  "mcpServers": {
    "tinyfn-regex": {
      "url": "https://api.tinyfn.io/mcp/regex/",
      "headers": {
        "X-API-Key": "your-api-key"
      }
    }
  }
}

See all regex tools available via MCP in our Regex MCP Tools for AI Agents guide.

Try the Regex Split API

Get your free API key and start splitting text with powerful patterns.

Get Free API Key

Ready to try TinyFn?

Get your free API key and start building in minutes.

Get Free API Key