❔ confused with regex pattern

I’m new to regex, and basically i have a .txt file that I read from and from that file i need a specific part. Example text file: Key: Something 1234789 …,, )8382&wkdlpfofek.. /Key So I need to extract everything between Key and /Key and put it into a string. How would I go about with creating the regex pattern for that?
20 Replies
phaseshift
phaseshift2y ago
Key:(.*)/Key You might need to add something for multi-line support
racoonknight
racoonknightOP2y ago
What is the difference between .* and .*?
Servator
Servator2y ago
.* is greedy match .*? is lazy match
mtreit
mtreit2y ago
Greedy vs non-greedy matching is the difference.
racoonknight
racoonknightOP2y ago
And to my understanding greedy means it will take the longest string that matches?
mtreit
mtreit2y ago
Does the file have multiple sections matching that pattern? Yes
racoonknight
racoonknightOP2y ago
no
mtreit
mtreit2y ago
Then greedy vs lazy shouldn't matter in this case
racoonknight
racoonknightOP2y ago
so purely for example if i had in my file Key: 1;kr;kr&: /Key Key: 17388848 Hajsifij^^^ Abbk 788 Uio /Key So greedy would take the longer one? Or would it take both and then I could use Groups[] to decide which i’d take?
mtreit
mtreit2y ago
No, Greedy would take both as one match You need parentheses for grouping
racoonknight
racoonknightOP2y ago
Ah I see, thank you
mtreit
mtreit2y ago
I personally would probably not use Regex for this
racoonknight
racoonknightOP2y ago
What would you use?
mtreit
mtreit2y ago
Two calls to IndexOf and one call to Substring probably.
racoonknight
racoonknightOP2y ago
But dont u need to know the specific index then?
mtreit
mtreit2y ago
I generally find regex across multiple lines finicky That's what IndexOf gives you
racoonknight
racoonknightOP2y ago
So you’d read the file into a string And then use indexOf to find the index of Key and /Key. And the substrin everything between?
mtreit
mtreit2y ago
Yes. I'm not necessarily claiming that is superior to regex...though it might perform better Not sure
racoonknight
racoonknightOP2y ago
I see. I didn’t think about that. Thank you
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server