I’m opening a file and finding the line I need, but then I have trouble creating a variable from the found string
70c 08:04:04.014 rexx TRACE 2203 8=4.4|9=892|35=J|49=ICE_SM_S|56=SM|34=280|70=0241608914160889|71=0|626=2|793=16|72=|466=1164266784|857=0|73=1|11=|37=1156426784|526=1156426674|38=1|198=1310883PTM|54=1|6=117.2100000000|336=R|625=P|55=B|461=FXXXXX|200=20120901|207=IFEU|53=1|30=ICE|453=2|448=SLM|447=C|452=7|448=FFC|447=C|452=12|75=20120210|60=20120310-09:04:04|77=O|58=CYU795|232=14|233=GL_TRADEJOBOUT|234=N|233=GL_ORDERJOBOUT|234=N|233=GL_TAKEN|234=0|233=GL_TRADETYPE|234=E|
This is the string and I want to assign it to a variable of tag198, so it would be
tag198 = '1310883PTMS'
Anything after | is not needed.
tag198 = line.match(/198=(.*)/)[1]
puts tag198
but that keeps all after 198; I need just the string prior to the |.
Your regular expression’s
*is greedy, and will consume all characters it can without stopping the rest of the expression from matching. There is nothing in the expression that tells ruby when to stop collecting characters.Look at regular-expressions.info. A partial fix for your problem would be to put a ‘|’ after your capture:
tag198=line.match(/198=(.*)\|/)[1] puts tag198The ‘|’ is escaped as it has special meaning in regexes otherwise. This doesn’t yet work though, because the
*can still consume ‘|’ characters, so long as it leaves one behind to match the ‘|’ in our expression. To fix completely, prevent the*from capturing any pipes:tag198 = line.match(/198=([^|]*)\|/)[1] puts tag198See results of this change here.