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
  • 3,249
  • 3
  • 32
  • 69
  • 1
    Whether the first or all occurrences, it is done with the code. The regex you need is `(?<!\S)[A-Za-z]++(?![:;.])` or `(?<!\S)[A-Za-z]+\b(?![:;.])`. Well, you may as well use `^.*?\K(?<!\S)[A-Za-z]++(?![:;.])` (PCRE) or get Group 1 in `^.*?(?<!\S)([A-Za-z]+)\b(?![:;.])` – Wiktor Stribiżew Jan 23 '20 at 23:26
  • 1
    @WiktorStribiżew yes, It works, I'm using the Perl and `(?<!\S)[A-Za-z]++(?![:;.])` worked perfectly. Thanks, man! – Soheil Pourbafrani Jan 24 '20 at 12:54

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
  • 607,720
  • 39
  • 448
  • 563