I've tried multiple options and can't find a solution. I have this list of words enclosed in square brackets:
[UPLOAD] [FOO] [DOG] [WEDNESDAY]
I have this text and I want to get the first match of that list of words with the square brackets included.
For example:
Lorem ipsum[LOAD]lorem ipsum Lorem ipsum "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation[another text] ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat[dummy] non proident,[FOO] sunt in culpa qui officia deserunt mollit anim id est laborum."
It should match [LOAD] (square brackets included) and no longer consider other words enclosed in square brackets (even if they are in the list).
Note that the word may or may not be preceded by a space, such as: lorem[LOAD]ipsum or lorem [LOAD] ipsum
One of the patterns I tested with is this:
(?:^|(?<= ))(\[LOAD\]|\[UPLOAD\]|\[FOO\]|\[DOG\]|\[WEDNESDAY\])(?:(?= )|$)
But it doesn't consider if the string doesn't have a space and if it finds a later one, it also matches (I only want the first occurrence).
CodePudding user response:
I think this can work if you want one single match/group :
(\[LOAD\]|\[UPLOAD\]|\[FOO\]|\[DOG\]|\[WEDNESDAY\])[^*]*$
CodePudding user response:
You can shorten the pattern, and capture the first match in a group an match the rest of the string so that there can be no more capture groups.
(\[(?:(?:UP)?LOAD|FOO|DOG|WEDNESDAY)\]).*
See a regex demo.
If you want to match all following lines instead of a single line, you can change .*
to [\s\S]*
To replace the match, you can switch the capture group by capturing the last part of the pattern, and use group 1 in the replacement.
For example
\[(?:(?:UP)?LOAD|FOO|DOG|WEDNESDAY)\]([\s\S]*)
See another regex demo.