❔ 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
Key:(.*)/Key
You might need to add something for multi-line supportWhat is the difference between .* and .*?
.* is greedy match
.*? is lazy match
Greedy vs non-greedy matching is the difference.
And to my understanding greedy means it will take the longest string that matches?
Does the file have multiple sections matching that pattern?
Yes
no
Then greedy vs lazy shouldn't matter in this case
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?
No, Greedy would take both as one match
You need parentheses for grouping
Ah I see, thank you
I personally would probably not use Regex for this
What would you use?
Two calls to IndexOf and one call to Substring probably.
But dont u need to know the specific index then?
I generally find regex across multiple lines finicky
That's what IndexOf gives you
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?
Yes. I'm not necessarily claiming that is superior to regex...though it might perform better
Not sure
I see. I didn’t think about that. Thank you
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.