Regex - Match multi-line content between double curly brackets - regex

I am am trying to refactor some code and need to use regular expression to find a large number of strings. An example string is like:
{{ Form::text('twitter', Input::old('twitter'),
array(
'class'=>'form-control ',
'placeholder'=>'E.g http://www.twitter.com/MyTwitterPage'
))
}}
I have managed to use \{\{(.*\s*Form::.*\s*)\}\} to match strings when they're on a single line, but it fails to match multi-line strings such as the above.
Also, I'm using PHPStorm's regex find feature if that's of any help.
Any help is much appreciated.

You can use
\{\{(\s*Form::\w*\((?:[^}]*(?:}[^}]+)*))}}
See the regex demo
It is basically the same as \{\{(\s*Form::\w*\([\s\S]*?)}}, but it uses an unrolled logic and is thus much effecient.

Preparse the file and remove newlines.
or set the /s flag to the regex and see if that works, not sure if php supports it.
/\{\{(.*\s*Form::.*\s*)\}\}/s

Related

Notepad++ RegEX how do I append a character based on start of the character and before a character?

I would like to append _OLD to the end of each strings that starts with SR_ but before the symbol ' or without it
For example my text is the following:
SR_Apple
When the 'SR_APPLE' rotten, we must discard it.
I would like the find and replace to do:
SR_Apple_OLD
When the 'SR_APPLE_OLD' rotten, we must discard it.
I have tried (SR_*)+$.*(?='\s) based on what i Learned but no luck so far. Please help. Thx in Adv
For simple cases you should be able to use
Find: (\bSR_[\w]+)
Replace: $1_OLD
(\bSR_.+?)('|$) and $1_OLD$2 could also work if the text after SR_ is more complex
The lookbehind you're using is only matching the string if it ends with a ' so it won't find the text not in quotes.
regex101 is a useful tool for debugging expressions

Regex to remove a whole phrase from the match

I am trying to remove a whole phrase from my regex(PCRE) matches
if given the following strings
test:test2:test3:test4:test5:1.0.department
test:test2:test3:test4:test5:1.0.foo.0.bar
user.0.display
"test:test2:test3:test4:test5:1.0".division
I want to write regex that will return:
.department
.foo.0.bar
user.0.display
.division
Now I thought a good way to do this would be to match everything and then remove test:test2:test3:test4:test5:1.0 and "test:test2:test3:test4:test5:1.0" but I am struggling to do this
I tried the following
\b(?!(test:test2:test3:test4:test5:1\.0)|("test:test2:test3:test4:test5:1\.0"))\b.*
but this seems to just remove the first tests from each and thats all. Could anyone help on where I am going wrong or a better approach maybe?
I suggest searching for the following pattern:
"?test:test2:test3:test4:test5:1\.0"?
and replacing with an empty string. See the regex demo and the regex graph:
The quotation marks on both ends are made optional with a ? (1 or 0 times) quantifier.

Using regex to replace all except specific string form

I'm looking to pull data from some code, to do that I thought I could use regex.
An example of the code I have is:
If IsNumeric(varCS) And IsNumeric(varGTV) And IsNumeric(varTV) Then
logInfo("GO")
shtDst.Range("D6").Value = shtSrc.Cells(varCS, varGTV).Value
shtDst.Range("G104").Value = shtSrc.Cells(varCS, varTV).Value
I would like the result to be:
"D6"
"G104"
The regex I've tried is:
.*(?:Range\((.*)\))?.*
and replacing with:
\1
However this results in just blank lines.
I've looked at lookahead and lookbehind but those seem to require a fixed length string.
I've been using Notepad++ plus various online regex test sites to verify my results.
Have a try with (don't make Range... optional):
Ctrl+H
Find what: ^(?:.*?Range\((.+?)\).*?|.+)$
Replace with: $1
This is working with the given example.
Try replacing [\S\s]*?("[^"]*?").* with $1\r\n (Example)

Notepad++ and delimiters: automatically replace ``string'' by \command{string}

Within Notepad++, I want to replace many instances of the type ``string'' by \command{string} where string can be any string of characters. I am fairly close to what I want to achieve with:
Find: (?<=``)(.*?)(?='')
Replace: \\command{\1}
There is still a problem. With the regex code above, instead of \command{string} I get ``\command{string}'' and I am not sure why the `` and '' are not removed?
It is because you are using lookaround assertions. Lookaround (zero-width) assertions only assert that a position can be matched and do not "consume" any characters on the string. You can use the below regular expression.
Find: ``([^']+)''
Replace: \\command{\1}
You need to wrap everything into a capture group and use that. NP++ seems to not support lookahead/behind, but you dont need that for this specific case anyway:
``([^']+)'' -> \\command{\1}
This will make sure it does not match two commands (longest match) in something like:
run ``ls -l'' or ``ls -a''

Regex : parsing a file location

I am trying to parse the file location using regex but I am getting extra characters when i use regex. The line that I am trying to parse is
A HREF="/MISO/getEQRFile;jsessionid=1JgnSTXhgvbpSYLVhp3h4ZpGltNpphxr1ncwlGnK3YXsh2phxKh9!794217179?entity=WEPM&nodeId=key0">EQR_WEPM_20131001_123354_M_082013.zip</a></b></td>
I need the text between the quotes. Currently I am using
^.+?<A\s*?HREF\s*?=\W(.+?.+?>) but it gives me the value
match.Groups[1].Value: /MISO/getEQRFile;jsessionid=1JgnSTXhgvbpSYLVhp3h4ZpGltNpphxr1ncwlGnK3YXsh2phxKh9!794217179?entity=WEPM&nodeId=key0">
which is an extra "> in the end. I would appreciate if someone can help me out.
Your regex sure is strange... Note that you should use a proper HTML parser if you're trying to parse HTML.
What's wrong with your regex is that you have > inside the capture, so that it'll get anything up to >.
Try using a negated class:
^.+?<A\s*?HREF\s*?="([^"]+)"
Or if you have single and/or double quotes:
^.+?<A\s*?HREF\s*?=(["'])(.*?)\1>
And use match.Groups[2].Value.
You can use a regex replace command and use:
(<A\s*?HREF\s*?=\W(.+?.+?>))([^<]*)(</a\s*>)
replacing by the 3rd group (the filename itself)
\3