xcaliber13 Posted June 27, 2019 Share Posted June 27, 2019 I have lines of data: Example: Encounter Info: Some different data here Hospital This I can find with no issues (?m)^\bEncounter\b.*\bHOSPITAL\b.* But what regex would I use to find only the lines that have Encounter Info: Some different data here but with no HOSPITAL at the end? So all the lines of data that I want to find begin with Encounter Info: some data here but no HOSPITAL at the end. How do I stop regex from finding the lines that do have HOSPITAL at the end of the line? I do really appreciate you guys that have a full understanding of regex. Thank you Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted June 27, 2019 Share Posted June 27, 2019 @xcaliber13 What's wrong with the pattern '^Encounter Info:' Maybe more samples are needed Do you need to extract some string from there? Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
xcaliber13 Posted June 27, 2019 Author Share Posted June 27, 2019 FrancescoDiMuro, thank you for the reply Here is an example: Encounter Info: NL some different data HOSPITAL < Do not want to find Encounter Info: NL still some different data HOSPITAL < Do not want to find Encounter Info: NL more different data HOSPITAL < Do not want to find Encounter Info: NL different data here < Want to find these lines Encounter Info: NL HOSPITAL < Do not want to find Encounter Info: NL and different data HOSPITAL < Do not want to find Encounter Info: NL HOSPITAL < Do not want to find Encounter Info: NL and more different data here < Want to find these lines All lines are in the same file. Link to comment Share on other sites More sharing options...
Developers Jos Posted June 27, 2019 Developers Share Posted June 27, 2019 Just create an small script with that data in it and the syntax you are testing which isn't working and post that here, as I am never feel the urge to have to retype a data and create the whole script for you. Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted June 27, 2019 Share Posted June 27, 2019 (edited) @xcaliber13 This should fit your needs: '(?m)^Encounter Info:.*(?<!HOSPITAL)$' Basically, the pattern means:(?m): Modifier. ^ and $ anchors match start and end of line;^: start of the line;Encounter Info: literally that string, case sensitive;.*: every character, from 0 to unlimited;(?<!HOSPITAL): negative lookbehind. It asserts that the line doesn't have HOSPITAL (case sensitive) at the end of the line;$: end of the line. Edited June 27, 2019 by FrancescoDiMuro Earthshine 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
xcaliber13 Posted June 27, 2019 Author Share Posted June 27, 2019 FrancescoDiMuro, Thank you! That pattern works perfectly. And I really appreciate that you explained why it works. Again thank you Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted June 27, 2019 Share Posted June 27, 2019 @xcaliber13 You're welcome Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
mikell Posted June 27, 2019 Share Posted June 27, 2019 @FrancescoDiMuro Nice, if you are sure that no unexpected trailing white(s) space(s) exist at the end of the line How would you manage such a case ? Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted June 27, 2019 Share Posted June 27, 2019 (edited) Hey @mikell! Always happy to see you here around. Well, in that case, a \s* would probably fit in there, isn't it? '^Encounter Info:.*(?<!HOSPITAL)\s*$' Edited June 27, 2019 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
mikell Posted June 27, 2019 Share Posted June 27, 2019 Did you try it ? Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted June 27, 2019 Share Posted June 27, 2019 @mikell Please, enlight me with your knowledge Humbly, I'm not really a fan of spaces in RegExes Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
mikell Posted June 27, 2019 Share Posted June 27, 2019 Hehe I asked this to you because the answer seems easy and obvious... but it is not ... and also because I had to fight a little to find it. There are certainly other ways but I like this one #Include <Array.au3> $txt = "Encounter Info: NL some different data HOSPITAL " & @crlf & _ "Encounter Info: NL still some different data HOSPITAL " & @crlf & _ "Other line: NL more different data " & @crlf & _ "Other line: NL more different data HOSPITAL " & @crlf & _ "Encounter Info: NL more different data HOSPITAL " & @crlf & _ "Encounter Info: NL different data here " & @crlf & _ "Encounter Info: NL HOSPITAL and something more, keep this ! " & @crlf & _ "Encounter Info: NL and different data HOSPITAL " & @crlf & _ "Encounter Info: NL HOSPITAL " & @crlf & _ "Encounter Info: NL and more different data here " ;$res = StringRegExp($txt, '(?m)^Encounter Info:.*(?<!HOSPITAL)\s*$', 3) ; case 1, fire lines ending with HOSPITAL + anything else ;$res = StringRegExp($txt, '(?m)^Encounter Info:(?|.*HOSPITAL(*SKIP)(*F)|.*)$', 3) ; case 2, fire lines ending with HOSPITAL + "0 or more white spaces" only $res = StringRegExp($txt, '(?m)^Encounter Info:(?|.*HOSPITAL\h*$(*SKIP)(*F)|.*$)', 3) _ArrayDisplay($res) FrancescoDiMuro 1 Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted June 27, 2019 Share Posted June 27, 2019 @mikell Spoiler mikell 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now