I'm trying to write a regular expression to extra the value which follows the word 'Total' but I'm not sure how to handle the carriage return which means i'm searching over 2 separate lines. does anyone know the best way to approach this ?
Taxes&Charges↵
↵
£ 35.97↵
↵
Total↵
£ 198.98↵
↵
£ 35.97↵
↵
↵
Total↵
£ 333.98
In regex you should use the
\r
to catch the carriage return and\r\n
to catch the line breaksYou should use regex option dot matches newline (if supported).
E.g. in
.NET
you could useRegexOptions.Singleline
for this. It specifies single-line mode. Changes the meaning of the dot (.
) so it matches every character (instead of every character except\n
).The next expression:
will match
£ 198.98
and£ 333.98
values from your test example.Add
s
modifier to your regex.s
means treat string as single line. Why don't you remove carriage return withstr_replace
first? Oops, no PHP tag.If you add s, to signify the single line, you can then (for example if windows) use
^Total\r\n£([ 0-9.]+)\r\n"
to extract the total values.(not tested, the brackets should ensure it appears as a group value)