How to Get Only First Character Match With Regex

Regex match only first char

Note that [(:*?)] regex matches 1 symbol that is either a (, or :, *, ?, or ) since the outer brackets form a character class (or a bracket expression in POSIX regex) where you define characters, or their ranges, that this construct can match, but it will match 1 char that belongs to the set/ranges.

The first : can be matched with


And replace with $1\t.

See the regex demo


  • ^ - start of string
  • ([^:]*) - Group 1 capturing 0+ chars other than : with a nregated character class [^:] (note we capture what we need to keep)
  • : - a literal : (note we match what we need to replace)

And the $1 in the replacement pattern refers to the value captured in Group 1.

Regular Expression Match only first character

I think this should work:


'6123': is any number.
For numbers starting with 6 it will match else it would return null.

Let me know if that works.

Regex matching only first character from value

Instead of .,use [^;] without lazy quantifier.

var re = new RegExp(pattern + "[^;]+",'g'); 
  • [^;] Anything except ;

REGEXP : find the first character after one specific other character


[^-]*- matches a sequence (possibly empty) of non-hyphens followed by hyphen. {3} after that matches it 3 times, so it finds the 3rd hyphen, and ^ at the beginning makes it start matching from the beginning of the string. The capture group (.) captures the next character.


Regex match first character once, followed by repetitive matching until end


You can not do it this way, with negated character classes and the pattern anchored at the start. For example for your va2__./), this of course won't match - because the first character is not in the disallowed range, so the whole expression doesn't match.

Your allowed characters for the first position are a subset, of what you want to allow for “the rest” - so do that second part first, replace everything that does not match [0-9a-zA-Z_.] with an empty string, without anchoring the pattern at the beginning or end.

And then, in the result of that operation, replace any characters not matching [a-zA-Z] from the start. (So that second pattern does get anchored at the beginning, and you'll want to use + as quantifier - because when you remove the first invalid character, the next one becomes the new first, and that one might still be invalid.)

PCRE Regex: Is it possible to check within only the first X characters of a string for a match

You can find your pattern after X chars and skip the whole string, else, match your pattern. So, if X=25:


See the regex demo. Details:

  • ^.{25,}\S+V.*(*SKIP)(*F) - start of string, 25 or more chars other than line break chars, as many as possible, then one or more non-whitespaces and V, and then the rest of the string, the match is failed and skipped
  • | - or
  • \S+V\s* - match one or more non-whitespaces, V and zero or more whitespace chars.

Regex - Check first character of every line

You can use



  • ^ - start of string
  • [^\r\n:-] - any char that is not CR, LF, : and -
  • [a-zA-Z0-9]{0,9} - zero to nine alnum chars
  • (?:\r?\n[^\r\n:-][a-zA-Z0-9]{0,9}){0,4} - zero to four occurrences of
    • \r?\n - CRLF or LF ending
    • [^\r\n:-][a-zA-Z0-9]{0,9} - any char that is not CR, LF, : and - and then zero to nine alnum chars
  • $ - end of string.

Regex: First occurrence between two character if there are two characters

You could optionally match the first part, and capture the second part in a group

^(?:.+?- )?(.+?) -
  • ^ Start of string
  • (?:.+?- )? Optionally match as least as possible chars until -
  • ( Capture group 1
    • .+? Match as least as possible chars
  • ) - Close group and match -

Regex demo

Or for example with the surrounding spaces and a negated character class matching any char except a newline or -


Regex demo

Related Topics

Leave a reply