Henry Tirla
Henry Tirla

Reputation: 69

Matching multiple string characters with regular expressions

`## (Chat room 1) Received connection request from client 1.

(CR 1) RR 1.

(Chat room 1) Accepted client 1.

(CR 1) AC 1.

(Chat room 0) Received connection request from client 2.

(CR 0) RC 2.

(Chat room 0) Rejected client 2.`Matching string Problem

The Image above clearly depict my problem. I am trying to match RR,AC and RC . I do not want a broad match but a specific match for either RR or AC or RC below is my attempted regex that works but its a broad match so long has one of these letters are found

(##[^\n]*\n)|(\(CR (\d+))\) [AC\RR\RC\] [0-9]+.

Additionally i tried using quotation enclosing each pair letter like this below;

(##[^\n]*\n)|(\(CR (\d+))\) ["AC"\"RR"\"RC"\] [0-9]+.

It is still a broad match

Upvotes: 0

Views: 223

Answers (2)

rs2687
rs2687

Reputation: 13

\)\s(.{2})\s

As pattern is similar have taken the ) space and group with range of 2 and end with space again .

Upvotes: 0

The fourth bird
The fourth bird

Reputation: 163207

You could match the leading ## followed by the rest of the line and use a single capturing group to match one of the 3 alternatives:

^##.*\n\(CR \d+\) (R[RC]|AC) \d+\.

Explanation

  • ^ Start of string
  • ##.*\n Match ##, the rest of the line and a newline
  • \(CR \d+\) Match (CR and space, 1+ digits and )
  • ( Capture group 1
    • R[RC]|AC Match RR or RC or AC
  • ) Close group 1
  • \d+. Match a space, 1+ digits and a dot

Regex demo

Upvotes: 1

Related Questions