2

I have a string like:

Location: FD130 New York GA Unit Serial Purchase Order Sales Order Make Model Amount Tax Total 12345566 Location: FD130 Roswell GA Unit Serial Purchase Order Sales Order Make Model Amount Tax Total 0130029964 

When i trying regex like to get the location of specified serial no.(34573). Regex: 'Location:(.*)?Unit(.*?)0130029964

Then it giving the whole string string with location and ending with 34573.

The expected output is FD130 New York GA when i pass 0130029964 serial id.

Is there any function in python for getting the first backward sub-string location from given serial no.

Avneet Singh
  • 50
  • 1
  • 6
Rushikesh Sabde
  • 700
  • 3
  • 16
  • The first group is giving the whole string as it is. – Rushikesh Sabde Aug 05 '20 at 08:56
  • `r'Location:\s*(\S+)[^.?!]*unit[^.?!]*\b34573\b'`? See https://regex101.com/r/AEpqfC/1 – Wiktor Stribiżew Aug 05 '20 at 08:57
  • Actually i tried this regex it giving the whole string as starting from location and ending with 34573 id. i am told that regex won't work here. – Rushikesh Sabde Aug 05 '20 at 08:59
  • See https://ideone.com/1WyKhM. With `m = re.search(rx, text, re.I)`, you get `Delhi`. Maybe you can use `r"Location:\s*([^.?!]*?)\s*format\s+unit[^.?!]*\b34573\b"`, too, see https://ideone.com/P04Clw – Wiktor Stribiżew Aug 05 '20 at 09:01
  • Try `\blocation:\s*((?:(?!location:|unit|format).)+) (?:format )?unit (?:(?!location:).)+\b0130029964\b` See https://regex101.com/r/26X0X0/1 and https://regex101.com/r/JM47JR/1 – The fourth bird Aug 05 '20 at 09:45
  • Is this what you are looking for `Location:(.*?)Unit(.*?)0130029964`? Demo [here](https://regex101.com/r/DuZTEZ/2). – marianc Aug 05 '20 at 09:48

1 Answers1

1

You can match location unit the first occurrence of unit using a tempered greedy token approach to not cross matching either location: unit or format.

Then optionally match format and match till the specific number without crossing matching location: again.

\blocation:\s*((?:(?!location:|unit|format).)+) (?:format )?unit (?:(?!location:).)+\b12345566\b

Regex demo

The fourth bird
  • 96,715
  • 14
  • 35
  • 52