Nick Price
Nick Price

Reputation: 963

PHP regex issue getting one string

I have data like the following

4  #VS   7 J9 C9 D9 I9 Z0 W8 S6 H0 LHRLAX 1115 1435      346 0E
5  $UN7107 J4 C4 D4 S. Y4 H4 Q4 B4 LHRLAX 1115 1435   *  744 0E

To get the data, I have the following regex

\A(\d)+[^#$]*?\#(?'code1'\p{Lu}{2})\s*(?'code2'\d{1,4})\b\s*(?<seat1>[A-Z][0-9.](?:\s+[A-Z][0-9.])*+)\s+(?<from>[A-Z]{3})(?<to>[A-Z]{3})\s+(?<other>.*\S)\s*

Now this works perfectly for the first line, but for the second line, the flight number starts with a $ instead of a # and I cant seem to match it. I have added a $ to the regex string without success.

How can I get this working?

Thanks

Upvotes: 0

Views: 33

Answers (1)

tmt
tmt

Reputation: 8614

\A matches start of string while you apparently want to match start of line with ^, and you also need to match both # and $ while your [^#$]*? was only matching "anything until one of those characters":

^(\d)+\s*[#$](?'code1'\p{Lu}{2})\s*(?'code2'\d{1,4})\b\s*(?<seat1>[A-Z][0-9.](?:\s+[A-Z][0-9.])*+)\s+(?<from>[A-Z]{3})(?<to>[A-Z]{3})\s+(?<other>.*\S)\s*

online test

Upvotes: 2

Related Questions