samiles
samiles

Reputation: 3900

Require that part of regex range must be present while other is optional

I have data like so:

01Jun18 324567645
01Jun18 Addfd
01Jun18 112 Word
01Jun18 fjfs7dfsfj
01Jun18 kdkd
01Jun18 0sdfnefk
01Jun18 skfks7sfsff
01Jun18 sfrfsf8srfsr

I want all lines except the first to match. The string can contain numbers, but it cannot except just numbers alone - there MUST be a letter present too.

So far I am using, but this matches all rows:

([\d]{2}[A-Za-z]{3}[\d]{2}) ([A-Za-z\d ]{1,})

Any ideas on how or if this can be achieved? I have tried using + to mark the A-Z as required

Upvotes: 0

Views: 32

Answers (2)

JoniJnm
JoniJnm

Reputation: 722

^([\d]{2}[A-Za-z]{3}[\d]{2}) [0-9 ]*[a-zA-Z]+[0-9a-zA-Z ]*$

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

You may use

preg_match_all('~^(\d{2}[A-Za-z]{3}\d{2})\h+(?!\d+$)([A-Za-z\d\h]+)$~m', $s, $matches)

You may even replace ([A-Za-z\d\h]+) with (.*) if you just want to match the rest of the line without caring about what chars there may be.

See the regex demo

Details

  • ^ - start of the line (due to m modifier)
  • (\d{2}[A-Za-z]{3}\d{2}) - Group 1: two digits, three ASCII letters, two digits
  • \h+ - 1 or more horizontal whitespaces
  • (?!\d+$) - a negative lookahead that fails the match if there are only digits to the end of the line
  • ([A-Za-z\d\h]+) - Group 2: one or more chars that are letters, digits, or horizontal whitespaces
  • .* - matches any 0+ chars other than line break chars as many as possible
  • $ - end of the line.

Upvotes: 0

Related Questions