Regex Skip First Character: Patterns, Pitfalls and How to Verify the Result

How to skip the first character with a regular expression: replace, capture group, lookbehind and \K patterns, plus Unicode, newline and empty-string pitfalls.

Skipping the first character with a regular expression means one of two things. You either remove that character from the text, or you match everything after it while leaving the original string unchanged. Both are short patterns, but newlines, empty strings, multiline mode and Unicode decide whether the character you skip is the one you meant. This guide covers what each approach does, how to choose one, and how to check the result in a regex tester before you rely on it.

What skipping the first character does to your text

The input is any string, such as a single value, a line of a file, or a whole multi-line document. The output depends on the approach. A replace operation returns a new string with the first character deleted. A match or extract operation leaves the text alone and gives you the remainder as a match or as a captured group. The scope matters as much as the pattern: an anchored pattern affects only the start of the string by default, but with multiline mode or a line-by-line tool it affects the start of every line.

Two terms are used throughout. An anchor is a zero-width marker for a position: ^ marks the start of the string, or the start of each line in multiline mode. The dot (.) matches one character, but in most engines it does not match a newline unless you enable a "dotall" or "single-line" flag, usually written s.

Choose an approach

Approach | Pattern | What you get | Typical support
Replace the first character | ^. replaced with nothing | A new string without the first character | Practically every engine and tool, including sed
Capture the rest | ^.(.) | The full match includes the first character; group 1 holds the rest | Practically every engine
Lookbehind | (?<=^.). | The match itself starts at the second character | JavaScript (ES2018 and later), PCRE, .NET, Java, Python re; not POSIX tools such as basic sed
Match reset | ^.\K.* | The match itself starts at the second character | PCRE, Perl, PHP preg functions, grep -P; not JavaScript or Python re

If you only need the shortened string, use replacement. It is the simplest pattern and the most portable. Use a capture group when you are already extracting other parts of the text. Use lookbehind or \K when the tool only shows or uses the whole match, such as a tester's highlight or grep -o, and you need that match to exclude the first character. Support details vary by engine version, so check your engine's documentation if you depend on lookbehind or \K.

Step by step: remove the first character

  • 1. Decide the scope: the start of the whole string, or the start of every line.
  • 2. Write the anchored pattern ^. and leave the replacement empty.
  • 3. If the first character could be a newline, enable the dotall flag (s) or use [\s\S] instead of the dot.
  • 4. Enable multiline mode (m) only if you want to remove the first character of every line.
  • 5. If the text can contain emoji or other characters outside the Basic Multilingual Plane, use your engine's Unicode mode. In JavaScript that is the u flag.
  • 6. Run the test strings in the verification section below and compare the output with what you expect.

Illustrative examples follow, one per language. The expected results come from documented engine behavior and were not run here, so check them in your own environment.

// JavaScript (example)
"Hello".replace(/^./su, "");          // expected: "ello"
"Hello".match(/^.(.*)/su)[1];        // expected: "ello"
"Hello".match(/(?<=^.).*/su)[0];     // expected: "ello"
# Python (example)
import re
re.sub(r'^.', '', 'Hello', flags=re.DOTALL)              # expected: 'ello'
re.match(r'^.(.*)', 'Hello', flags=re.DOTALL).group(1)   # expected: 'ello'
# Shell (example)
sed 's/^.//' input.txt             # removes the first character of every line
echo "Hello" | grep -oP '^.\K.*'   # expected output: ello

Note that sed works line by line, so the sed example always applies to every line, with or without a multiline flag. In GNU sed and grep, whether a multi-byte UTF-8 character counts as one character or several bytes depends on the locale settings.

Skip the first character only when it is a specific one

Often the goal is to drop a known prefix, such as a leading # or a sign, and not whatever character happens to come first. Name the character so that other text is never touched. For example, ^# removes a leading hash only if it is there, and ^[+-] removes a leading plus or minus sign. Escape characters that have special meaning in regex. For instance, ^. matches a literal leading dot, while ^. matches any character.

Pitfalls that change the result

  • Empty string: ^.(.*) does not match "" at all, so code that reads group 1 can fail. Replacing ^. returns the empty string unchanged, which is usually safer.
  • Newline first: without the dotall flag, ^. does not match a leading newline, so the text comes back unchanged. That is easy to mistake for a bug elsewhere.
  • Multiline mode: with m (and g in JavaScript), ^. removes the first character of every line, not just the first line.
  • Invisible first characters: a leading space, tab, or byte order mark (U+FEFF) counts as the first character. If text pasted from a file seems to lose nothing, the removed character may have been an invisible BOM.
  • Emoji and surrogate pairs: in JavaScript without the u flag, the dot matches one UTF-16 code unit. In "\u{1F600}abc", replacing /^./ leaves half of the emoji behind as a broken character. With /^./u, the whole emoji is removed and "abc" remains.
  • Combining characters: a character such as é can be stored as e followed by U+0301 (combining acute accent). The dot matches only the e, which leaves the accent attached to the next character. To skip one user-perceived character, use \X, which matches a grapheme cluster, in engines that support it, such as PCRE, Perl, and the third-party Python regex module.

Verify the result

Paste these inputs into your regex tester or script and write down the result you expect for each before you look. Escape sequences are written out so the invisible characters are visible here. Enter them in their real form when you test.

Test input | What it checks | Expected result with ^. removed (dotall and Unicode modes on)
Hello | Basic case | ello
(empty string) | No first character | (empty string), with no error
A | Single character | (empty string)
\nabc | Leading newline | abc; without dotall, unchanged
abc (leading space) | Invisible whitespace | abc
\uFEFFabc | Byte order mark | abc
\u{1F600}abc | Emoji outside the BMP | abc; without the u flag in JavaScript, a broken half-character plus abc
e\u0301tude | Decomposed accent | \u0301tude when using the dot; tude when using \X

Also compare lengths. On ordinary text the output should be exactly one character shorter than the input. If the difference is zero, the pattern did not match. A difference of two or more, or an output that starts with a stray accent or a replacement symbol, means the engine and the text disagree about what one character is.

When regex is not the best tool

If you control the code and only need to drop a fixed first character, string slicing does the same job and is easier to read. Examples are s[1:] in Python and s.slice(1) in JavaScript. The same Unicode caveats apply, because slicing also counts code points or code units, not grapheme clusters. Regex is the better choice when the character is conditional, when the operation is part of a larger pattern, or when you are working in a tool that only accepts patterns.

Gaps in this guide

No tool-specific evidence was supplied. This guide therefore does not cover any particular online regex tester's flags, interface, or engine version, and none of the examples above are verified output. Engine support for lookbehind, \K, and \X is described in general terms and can differ between versions. Check it against the documentation for the engine you actually use.