Why using (.|\n)*? is a bad idea? [duplicate] - regex

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Regex search with pattern containing (?:.|\s)*? takes increasingly long time
(1 answer)
Closed 3 years ago.
A few times I saw regex experts say that using (.|\n)*? is a really, really bad idea.
Well, I do understand that it's better to replace it with the .* and use the /s flag. But sometimes the flags are not available, for example, when using regex within a text editor or other software with limited regex functionality. Thus, using something like (.|\n)*? might be the only option for multi-line matching.
So, what are the reasons to always avoid (.|\n)*??

Related

Regex match repeating pattern [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I'm trying to match e-mails with a string like:
s.te.e.ve.s.mit.h.p#gmail.com
Effectively I'm after any repeating pattern (of at least 4 times) of a string of characters followed by a period, with the last before the email domain not having a period.
I'm not great with Regex, but so far I've only come up with:
[aA-zZ\.]{4,}[aA-zZ]#.*
This matches what I need, however it also pulls more than I'd like.
Any advice?
Thanks for the help, I see now where I was making the mistake. Wiktor's answer seemed to work the best, though for some reason it would time out in Redshift if I didn't put . in brackets. [.]
The expression which appears to work correctly is:
^([a-zA-Z][.]){4,}[a-zA-Z]#.*

Searching for word with lower case and upper case in regex [duplicate]

This question already has answers here:
Using alternation or character class for single character matching?
(3 answers)
Closed 3 years ago.
I have started using regex and am trying to write an expression which searches for a specific word in both upper and lower case.
For example, to search for 'zebra' or 'Zebra', I have created the string \b(z|Z)(ebra)\b.
This works, but is there a more elegant way to do this?
No, there isn't anything much better than you have now. You could drop the parentheses though and simplify it a bit:
\b[Zz]ebra\b or \b([Zz]ebra)\b if you need a capturing group.

Finding Repeated Patterns using a Regular Expression [duplicate]

This question already has answers here:
How to find overlapping matches with a regexp?
(4 answers)
Closed 3 years ago.
I'm trying to extract the repeated pattern from a string.
For example with something like "112112112112" I would want to end up with "112".
I've been having problems where I either end up with "1" or "112112".
The patterns can be of any size.
Here's an example of the kind of expressions I've been playing around with.
^(.+)(?=\1)
There are repeated patterns with different sizes, if 3 would be desired, for instance, we'd use a quantifier for that, such as:
(.{3})(?=\1)
Demo 1
or
(.{3,5})(?=\1)
Demo 2

How to extract body of for-loop? [duplicate]

This question already has answers here:
Can regular expressions be used to match nested patterns? [duplicate]
(11 answers)
Closed 6 years ago.
There are multiple C++ files. I need to extract the body of for-loop from these files.
Is there an easy way to do this maybe using grep. Consider there are no nested for loops.
Without parsing the entire file, the answer is no.
for-loops are comprised of a context-free grammar and, as such, cannot be matched by a regular expression.
A more involved approach is to use grep to search for the beginning of a for-loop (for follow by optional whitespace followed by a lpar) then manually find the closing curly.
Unfortunately parsing C++ is Turing Complete, so unless there's some cute flag to pass to your compiler, you're hosed.

How can I generate text from a RegEx? [duplicate]

This question already has answers here:
Using Regex to generate Strings rather than match them
(12 answers)
Reverse regular expression, create string from regex
(1 answer)
Closed 9 years ago.
Or "How can I RegEx in reverse?"
specifically I want to take a regex such as wks[0-9][0-9][0-9]
and create a list such as wks001,wks002,wks003, etc
I know the easiest way in this example would be to simply increment the number through addition, but say I want it to be even more sophisticated later on such as [0-9abc] I'd like to use a more sophisticated tool.
preferable would be some windows capable scripting tech, such as vbscript/powershell but I'm open to other alternatives. I guess I kind of thought this might be something that is done all the time by random number generators and such and would be a programming staple, but I lack the understanding to phrase it correctly I think.