Ali
Ali

Reputation: 19682

Regular expression matching beginning of line OR a set of characters

At first I thought this must be very easy, I am just overlooking something, but so far with my limited knowledge of regex I can't figure this out,

I have a regex like [some characters]MYNAME actual thing is:

    rx = rx + `[ ,\t,,\,,\(,=,@,\s]+(MYNAME)`

I want this regex to also detect a line that starts with MYNAME. So the question is, is there a way to add ^ inside [] with other things? or to OR the ^ with a [some characters]?

I can't make it work either with javascript or golang. If there are differences related to this matter, I am interested in the golang specific solutions.

Upvotes: 1

Views: 134

Answers (1)

Rohit Jain
Rohit Jain

Reputation: 213261

You can use alternation. Also, there are some unnecessary characters in your character class:

  • I don't know what those commas are supposed to do? Did you intent them to act as separator? If yes, remove them.
  • Also, you don't need to escape ( in a character class.
  • Since you have added \s, you don't need to add \t and " " separately.

So, your regex can be simplified to:

"(?:[(=@\s]+|^)(MYNAME)"

Upvotes: 4

Related Questions