Ir para o conteúdo

Lição 12 · Parsing and Transforming

Text To Columns and RegEx

Split fields with the Alteryx Text To Columns tool, then use the RegEx tool to replace, tokenize and parse text with patterns that you test with care.

Parsing means breaking one value into useful pieces: a full name into parts, a log line into columns or a list of tags into rows. Designer has two workhorses for this: Text To Columns for simple separators and RegEx for patterns.

This lesson shows both, and when to prefer one.

Text To Columns

Choose the field to split and the delimiter characters. Then choose an output shape:

  • Split to columns: you set the number of columns, for example 3.
  • Split to rows: each piece becomes a new row. Keep an ID column so you can tell which original record each row came from.

If you list several delimiter characters, each one acts as a separate delimiter. You can also decide what happens when a value has more pieces than columns, such as keeping the extra text in the last column, raising an error or dropping it with a warning. Check how your version words these choices.

Python Stand-in: split to columns and to rows

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Splitting by a comma fails when a comma is part of a value, as in "Smith, Jr., John". If your text file uses quoting, handle the quotes at input time instead of splitting afterward.

The RegEx tool

A regular expression is a pattern language for text. Our Python regular expressions lesson introduces the basics. The Designer help for recent versions lists these output methods:

Method What it does
Replace Replaces matched text; you can refer to captured groups
Tokenize Keeps the parts that match (or only the marked group, if the pattern has one), split into columns or rows
Parse Puts each captured group into its own new field

The help for some versions also lists a Match method, which adds a column that shows whether the text matched. Check which methods your version has.

A group is a part of the pattern inside parentheses. In Parse, each group becomes a field and you set its name, type and size. In Replace, you reference groups in the replacement text, for example $1 for the first group.

Here is a pattern for an order code such as ORD-2026-00417:

TEXT
^ORD-(\d{4})-(\d+)$

With Parse, group 1 becomes year and group 2 becomes sequence.

Pattern building blocks

Pattern Meaning
\d A digit
\w A letter, digit or underscore
\s Whitespace
. Any character
*, +, ? Zero or more, one or more, zero or one
Exactly four
[A-Z] One uppercase letter from the set
^, $ Start and end of the text

Also check the case option in the tool. Many versions of the help describe a case-insensitive setting that is selected by default. With it on, [A-Z] also matches lowercase letters.

Python Stand-in: replace, tokenize and parse with Python re

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the second code did not match. In a workflow, failed parses produce nulls. Always count them and review a sample.

Greedy matching

By default, * and + take as much text as possible. In <a><b>, the pattern <.+> matches the entire string, not just <a>. Add ? to make it lazy: <.+?>. Or be specific: <[^>]+>.

Python Stand-in: greedy, lazy and specific patterns

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In formulas you can use regex functions such as REGEX_Match, REGEX_Replace and REGEX_CountMatches. REGEX_Replace replaces all matches, not just the first. For structured formats, prefer the dedicated tools: XML Parse for XML and JSON Parse for JSON (in the Developer category). Regex is the wrong tool for nested data.

An email or phone pattern can check shape, not truth. A value can match a pattern and still not exist.

Building a pattern step by step

Long patterns are hard to read and harder to fix. Build them in small steps:

  1. Collect real examples, including bad ones that should not match.
  2. Match the simplest fixed part first, such as the prefix ORD-.
  3. Add one piece at a time and test after each step.
  4. Add anchors ^ and $ when the whole value must fit the pattern.
  5. Add groups only around the parts you want to extract.

Keep the examples in a small test workflow, so that anyone who changes the pattern can check the old cases again. Add an annotation that explains the pattern in words, for example "ORD, four-digit year, running number". If a pattern needs a paragraph to explain, consider splitting the work into two simpler steps with Text To Columns followed by a Formula.

Names and addresses

Splitting personal names by spaces looks easy and fails often. Many names have several family names, particles or only one part, and the order differs between cultures. Unless a task really needs the parts, keep the full name as it was given. When you must split, test with varied examples and keep the original column.

Practice task

Invent ten product labels such as TOOL-0042 (blue), with some bad ones: a missing color, a lowercase code, extra spaces and a code with too few digits. Decide which labels must be accepted.

Write a pattern with three groups for the category, number and color. Test it on all ten and count the rows that fail. Adjust the pattern or add a cleaning step until exactly the labels you chose are accepted. Do not make the pattern so loose that bad labels pass. Send rejected labels to their own output with the original text.

Common mistakes

Do not assume every row matches. Add a Filter after parsing to catch null results, and send them to a review output.

  • Splitting a quoted CSV field by comma.
  • Forgetting to escape special characters such as . or ( when you mean the literal character.
  • Letting the default field size truncate parsed values.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. When you split a field to rows, why keep an ID column?

    Without a key, the pieces cannot be traced back.
  2. 2. Which RegEx method puts each captured group into a new field?

    Parse creates a field for each marked group.
  3. 3. On the text "<a><b>", what does the greedy pattern <.+> match?

    Greedy quantifiers take as much as they can.
  4. 4. What should you check after parsing a field?

    Non-matching rows produce missing values that you must review.

Summary

  • Text To Columns splits by delimiter into columns or rows.
  • RegEx can replace, tokenize and parse with groups.
  • Watch greedy matching, escaping and case options.
  • Count and review rows that do not match, and use XML or JSON tools for structured data.

Next: Parsing and Calculating Dates

Seu progresso fica salvo neste navegador. Não precisa de conta.