1

I'm gonna select the first occurrence of an only-alphabet string which is not ended by any of the characters ".", ":" and ";"

For example:

"float a bbc 10" --> "float"
"float.h" --> null
"float:: namespace" --> "namesapace"
"float;" --> null

I came up with the regex \G([A-z]+)(?![:;\.]) but it only ignores the character before the banned characters, while I need it to skip all string before banned characters.

Soheil Pourbafrani
  • 2,921
  • 2
  • 25
  • 63

1 Answers1

1

You may use

/(?<!\S)[A-Za-z]++(?![:;.])/

See the regex demo. Make sure not to use the g modifier to get the first match only.

One of the main trick here is to use a possessive ++ quantifier to match all consecutive letters and check for :, ; or . only once right after the last of the matched letters.

Pattern details

  • (?<!\S) - either whitespace or start of string should immediately precede the current location
  • [A-Za-z]++ - 1+ letters matched possessively allowing no backtracking into the pattern
  • (?![:;.]) - a negative lookahead that fails the match if there is a ;, : or . immediately to the right of the current location.
Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476