How to replace text with regex? - vb.net

How would I write regex in Visual Studio's Find and Replace to change all instances of the following text:
If (whatever control and overloads) = "" then
To this:
String.IfNullorEmpty(whatever control and overloads) then

Search for:
If (.*) \= "" Then
And Replace with:
String.IfNullOrEmpty($1) Then
Although, what I think you actually meant was:
If String.IsNullOrEmpty($1) Then
The trick is in simply surrounding the desired part of the pattern in parenthesis to make it a RegEx group. Then in the replace string you can reference the groups with the $ syntax.

Related

intellij idea incorrect processing of regex: "regular expression matches empty string"

When regex contain simple .* is does not find anything.
Example from picture: we're taking whole line into capturing group, and in replacement just to prepend a to match.
What is this behavior? Is there some flag to turn off intellij-idea interpretation of my regexes? What is so wrong with empty string match in first place??? Also .* is greedy, so it can match empty string only if there is empty line, which there isn't. Switch "search in selection makes no difference".
As a workaround for this problem, you can use a regular expression like the following: ^(.*\n)

What's the regular expression to replace (., space, -, ) in a string using Labview

I am trying to replace some unnecessary characters in a string in LabView. For example in the following string, "he llo-hOW-are.You?" I want to replace space, . and - with an Underscore _.
The output shall be "he_llo_hoW_are_You?"
I tried "[\ \.\-]" and many more things but nothing seem to work for a regular expression.
I am using Search and Replace function and I am putting the Regular expression in the Search String field.
Thanks.
Did you make sure to right click the function and check the Regular Expression option? The function won't work with regexes otherwise (as the help for it says).
This seems to work just fine here:
Make sure you set the Regular Expression in the "Search and Replace String" right click menu.
It should work with the regular expression you are trying to use. Or you can use [\ .-] in the Regex as well.
This logic will apply all special Characters.

REGEX check only when start of line

Suppose we want to keep the entire line of a string only if a particular word say e.g 'test' appears at starting of line.
If it appears anywhere then the entire line should be removed
e.g
if function_test()=5; //here this entire line should be removed
test sample =5; //here this entire should be there
From Oracle 10g R2 on you should be able to use the anchor \A to require the match at the beginning of the string (will only work for single-line strings thus).
http://www.regular-expressions.info/oracle.html
What do you mean by keep / remove lines? Where is this regex supposed to run? I.e. is it a part of an SQL command, or part of a grep, or sg else?
Regarding SQL you can use LIKE operator:
WHERE line LIKE 'test%'
You can use substring too:
WHERE substring(line, 1, 4) = 'test'
Using grep or any other language, you can specify start of line, e.g.:
grep '^test' bigfile.txt
Try...
...
WHEN REGEXP_LIKE(string,'^test','i') THEN
//this is a good line, do what you want or return string;
END
...

Using groups in OpenRefine regex

I'm wondering if it is possible to use "groups" in ReGeX used in Open Refine GREL syntax. I mean, I'd like to replace all the dots followed and preceded by a character WITH the same character and dot but followed by a space and then the character.
Something like:
s.replace(/(.{1})\..({1})/,/(1).\s(2)/)
It should, but your last argument needs to be a string, not a regular expression. Internally Refine uses Java's Matcher#replaceAll method which accepts a string argument.
I think I found out how to deal with this. You need to put $X in your string value to address a Xth capture group.
It should be like this:
s.replace(/.?(#capcure group 1).?(#capcure group 2).*?/), " some text $1 some text $2 some text")

Tool to format lines of text into array

I frequently come across this problem. I have a file:
something
something2
something3
which I want output as:
"something","something2","something3"
any quick tool for this, preferably online?
If its just a one off thing, it'd be pretty easy to just do it with a search & replace in any advanced-ish text editor...
For example in notepad++:
Do a Replace (CTRL+H)
Set "Search Mode" to "Extended"
Find: \r\n
Replace with: ","
(of course you'll need an extra quote at the very start & very end of the file).
If you need to do it more than once, writing a small script/program that did a regular expression replace over the file would be fairly straight forward too.
Edit: If you really wanted to do it online, you could use an online regular expression tester (in this case you want to use \n as the regex and "," as your replace pattern, leaving the other settings alone).
A quick Python hack?
lines = open('input.txt').xreadlines()
txt = ','.join(['"%s"' % x for x in lines])
open('output.txt', 'w').write(txt)