Alexander Solonik
Alexander Solonik

Reputation: 10230

Regex to match plain string with only alphabets as well as optional dot character as beginning of match

I have the following regex:

[:?\.](.+)[\|]?

Which successfully matches strings like the below:

The data.mws auth token field is required| lalafa daga.lallala

Or

The data.mws auth token field is required

But I'd also like it to match strings like :-

This is an error

how do I make the [:?\.] part optional ? I tried [:?\.]? problem is for strings like The data.mws auth token field is required| lalafa daga.lallala it will match the the data part also, how do I solve this issue.

Upvotes: 0

Views: 91

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

You can use

/(?:^(?!.*[:?.])|(?<=[:?.]))(.+)\|?/

See the regex demo

Details

  • (?:^(?!.*[:?.])|(?<=[:?.])) - either start of string not followed with :, ? and . after any zero or more chars other than line break chars, as many as possible, or a location immediately preceded with :, ? or .
  • (.+) - Group 1: any zero or more chars other than line break chars, as many as possible
  • \|? - an optional | char.

Upvotes: 1

Related Questions