0

i have a question, need to extract a value before a defined string, but i can't, for example need to extract the value "Pudahuel", this word (dinamic) It is before the word "Horario", any suggestions?

 Orden de Trabajo
                                                            N° 0200087663


                                                                 Pudahuel Horario Atención: LU a VI de 08:30 a 13:00 y 14:30 a 19:00
Datos de Recepción:

example link: https://regex101.com/r/zN2vG6/29

2 Answers 2

1

Your (.*)(?=Horario) matches 0+ chars other than a newline (because you did not use the DOTALL modifier) before a Horario text. Note there is a linebreak between the words you need.

Use the following regex

(\w+)\s*Horario

See the regex demo

The value you need will be in Group 1. Instead of \w+ (one or more word chars, those from [a-zA-Z0-9_] range) you may use [a-zA-Z]+, or even \p{L}+ (any 1+ letters).

NOTE: When you have Unicode chars in the patter/string, you need to use the /u modifier:

/(\w+)\s*Horario/u

or your suggested pattern (a bit enhanced):

/N°\s+(\S+)/u
            ^
Sign up to request clarification or add additional context in comments.

9 Comments

bro you rock!!!!!!!! thanks, and if the value is "LA SERENA" instead of "PUDAHUEL" (because the text are dinamic), any idea? :'v
It depends on the criteria. If you want to match a chunk of space separated ALLCAPS words before Horario, use \b([A-Z]+(?:\s+[A-Z]+))\s*Horario.
Or, if you need to match the whole string free from the initial whitespace before an Horario word that appears at the start of the next line, use /^\h*(.*)\R+Horario/m.
thanks!!, now i have a problem with the ordinal character (°), because need to extract the value (0200087663, the number is dinamic), but the cron doesnt recognize the char (°), any idea?
No idea what input you have. Please note that you may use the last pattern in the previous comment to just match anything. Also, you can always include any chars into character classes. Say, [\w°] will match a word char and a °.
|
0

Your initial string could contain vertical/horizontal tabs, use the following regexp:

(\b[\w ]*?)[\v\h]?(?=Horario)

DEMO link

2 Comments

thanks!!, now i have a problem with the ordinal character (°), because need to extract the value (0200087663, the number is dinamic), but the cron doesnt recognize the char (°), any idea?
This is a case where one really does not need to use a lookaround.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.