Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I match whitespace around a text field without capturing it?

Quick answer

Put the surrounding whitespace and the field in separate capture groups, then use only the field group when extracting the value. For a nonempty field defined as one or more non-whitespace characters, use (?P<before>\s*)(?P<field>\S+)(?P<after>\s*) in Python, or /^(\s*)(\S+)(\s*)$/ in JavaScript. The meaning of \s depends on the regex engine, so choose the whitespace scope your input requires.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a line with spaces or tabs around a value. How do I match those spaces without making them part of the value I extract?

Geez

Use separate capture groups: one for whitespace before the field, one for the field, and one for whitespace after it. Then extract only the field group. In Python, for example:

(?P<before>\s*)(?P<field>\S+)(?P<after>\s*)

Here, \S+ defines the field as a nonempty run of characters that are not whitespace. The before and after groups can match whitespace, but they are not part of the field group. In Python Unicode string patterns, \s follows Python’s str.isspace() definition; with re.ASCII, or with byte patterns, it is limited to [ \t\n\r\f\v]. [S1]

This pattern describes the groups; use your language’s match or extraction method to retrieve the named field group.

Follow-up question

Can you show what the groups contain for a concrete line?

Geez

Suppose the input is invoice-42\t. With the Python pattern above, the intended captures are:

  1. before: three leading spaces
  2. field: invoice-42
  3. after: one tab

Retrieve the field group rather than the whole match. The whole match includes the surrounding whitespace, while the field capture contains only the value as defined by this pattern. The pattern itself is not anchored, so if you need to require that the entire input line consists of these three parts, use a matching method or pattern form that enforces that in your chosen API.

If the value may itself contain spaces, \S+ is too narrow. Define the field’s boundaries first, then adapt the pattern.

Follow-up question

What would the equivalent pattern look like in JavaScript, and does it treat whitespace exactly the same way?

Geez

For a single line, a JavaScript pattern with three capture groups is:

/^(\s*)(\S+)(\s*)$/

The captures represent leading whitespace, a nonempty run of non-whitespace characters, and trailing whitespace. In JavaScript, \s matches whitespace and line-terminator characters, and \S is its complement. Do not assume its character set is identical to Python’s Unicode str.isspace() set. [S2]

The pattern separates the captures; how you retrieve them depends on the JavaScript operation you use. Check the behavior against the exact input and API, especially if your data can contain line terminators.

Follow-up question

How should I choose the pattern if my input has Unicode spacing or I want to replace the line with just the field?

Geez

Decide what you mean by whitespace before relying on \s: perhaps you want spaces and tabs only, or perhaps you want the regex engine’s broader whitespace class. Then try representative inputs, including any spacing characters your data may contain. Python’s documented \s behavior depends on pattern type and flags, and JavaScript’s documented character class should not be assumed identical. [S1] [S2]

For extraction, select the field capture. To produce a line containing just the field, construct the result from that capture rather than retaining the whole match. Replacement syntax varies by language API, so check the documentation for the specific operation instead of assuming one notation works everywhere. Also decide what should happen with a blank line: \S+ requires at least one non-whitespace character, so the examples do not match an empty field.

A practical checklist

  • Decide whether the field is one non-whitespace token or may contain spaces.

  • Choose the intended whitespace scope; do not assume \s behaves identically across engines.

  • Capture leading whitespace, the field, and trailing whitespace separately.

  • Check representative input, including any spacing characters your data may contain.

  • Retrieve or construct the result from the field capture, and handle blank lines explicitly.

A few more useful answers

Does `\S+` match a field that contains spaces?

No. In these examples, \S+ means one or more consecutive non-whitespace characters. If the field can contain spaces, define its boundaries differently and adapt the pattern.

Will these patterns match an empty field?

No. \S+ requires at least one non-whitespace character. Decide how an empty field should be handled and use a pattern suited to that requirement.

Can I use the same replacement syntax in Python and JavaScript?

Do not assume so. These examples show how to separate captures; replacement syntax depends on the language API, so check the documentation for the operation you use.

Sources

References used for this guide.

  1. [S1] Python 3.15.2: Regular expression operations
    docs.python.org · Checked
  2. [S2] MDN: JavaScript regular-expression character class escapes
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work