I want to replace a LabelFor command, using the Visual Studio replace tool
From:
#Html.LabelFor(m=>m.myRecords.First().Column1)
#Html.LabelFor(m=>m.myRecords.First().M.M.Column2)
To
#Html.LabelFor(m=>m.myRecords.First().Column1, null, Model.Status)
#Html.LabelFor(m=>m.myRecords.First().M.M.Column2, null, Model.Status)
I have used "Column1" and "Column2" as examples of 2 columns which should remain unchanged after the replacement ie addition of :
, null, Model.Status)
to end of statement.
I suspect I need a regular expression to identify the last ")" and then to replace with
, null, Model.Status)
Thoughts appreciated.
Thanks.
Basically you have to match the last brace, with \)$ (globally and multiline)
Then replace with your string , null, Model.Status)
Regexr solution
Related
So i am trying to search php files using preg_match_all to find method usages in there and take parameter values from it.
For example if i there is method call like:
method("some text", null, "third param")
I want to get some text, null, and third param to an array.
I was able to get first parameter, bug having problems for second and third one.
What i tryed:
\(method)\([\'"](.*?[ \na-zA-Z0-9_-]+)[\'"]\im
https://regex101.com/r/dC8wV6/2
You can try
method\s*\(((?:\s*(?:['"][^'"]*['"]|\w+)\s*(?:,|(?=\))))*)
to capture the parameters in group 1, then split them on , or ,(?=(?:(?:[^'"]*['"]){2})*[^'"]*$) to get an array of parameters.
This pattern comes with the usual pitfalls, i.e. commas in strings (method(" , ")) or mismatched quotes (" ' ") or escaped quotes (" \" ") will trip it up. Those issues could be fixed, but I think the regex is long enough as is.
I am trying to replace all the "." in a specific column of my data frame with "/". There are other characters in each cell and I want to make sure I only change the "."'s.
When I use gsub, I get an output that appears to make the changes, but then when I go to View(), the changes are not actually made...I thought gsub was supposed to actually change the value in the data frame. Am I using it incorrectly? I have my code below.
gsub(".", "/", spy$Identifier, ignore.case = FALSE, perl = FALSE,
fixed = TRUE, useBytes = FALSE)
I also tried sub, but the code I have below changed every entry itself to "/" and I am not sure how to change it.
spy$Identifier <- sub("^(.).*", "/", spy$Identifier)
Thanks!
My recommendation would be to escape the "." character:
spy$Identifier <- gsub("\\.", "/", spy$Identifier)
In regular expression, a period is a special character that matches any character. "Escaping" it tells the search to look for an actual period. In R's gsub this is accomplished with two backslashes (i.e.: "\\"). In other languages, it's often just one backslash.
I could use some help writing a regex. I have the following text:
DEFINE BROWSE BW_SC20SDAN
&ANALYZE-SUSPEND _UIB-CODE-BLOCK _DISPLAY-FIELDS BW_SC20SDAN C-Win _FREEFORM
QUERY BW_SC20SDAN NO-LOCK DISPLAY
ZTYACC.prime COLUMN-LABEL "" FORMAT "X(35)"
ZUNACT.sec COLUMN-LABEL " " FORMAT "X(30)"
INFDON.sep COLUMN-LABEL "" FORMAT "99/99/9999"
IF INFDON.top THEN "S" ELSE (IF INFDON.REPORT THEN "R" ELSE (IF INFDON.prime <> "" THEN INFDON.prime ELSE "")) COLUMN-LABEL "R" FORMAT "X(1)"
/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME
WITH SEPARATORS SIZE 83.57 BY 5.08
BGCOLOR 15 FGCOLOR 1 FONT 6 FIT-LAST-COLUMN.
I have to find this whole block in a text file, so far I have this regex:
(?:DEFINE|DEF)\s([\w\s]*)BROWSE\s+([\w-]+)\s+([^.]*)\.
My problem is that it selects only this :
DEFINE BROWSE BW_SC20SDAN
&ANALYZE-SUSPEND _UIB-CODE-BLOCK _DISPLAY-FIELDS BW_SC20SDAN C-Win _FREEFORM
QUERY BW_SC20SDAN NO-LOCK DISPLAY
ZTYACC.
When I want to select until the final point. Basically, the rule I want to apply is "until next dot followed by \s".
But I can't figure out how to write this regex.
Allow "non-dot" [^.] OR "dots not followed by space" \.(?!\s):
DEF(INE)?\s([\w\s]*)BROWSE\s+([\w-]+)\s+(([^.]|\.(?!\s))*)\.
Note also the simplification of the leading term.
Probably the most readable way to do that is
(?:DEFINE|DEF)\s([\w\s]*)BROWSE[\S\s]+?\.\s
You turn the + operator lazy with ?, meaning by default it matches everything until it hits the first period followed by a space.
If you have the option to use an ungreedy regex library, the simplest yet closest to what you specified would be
DEFINE\s+BROWSE.*?\.\s
Note, however, that the trailing whitespace may not be there at the end of your input text, leaving the last statement unmatched.
You may find it useful to have a lexer (scanner) like flex or ANTLR tokenize your string. This approach has the advantage that the lexer takes care of the white space and lets you specify the form of the block of interest in more detail.
I am writing a script to extract and convert SQL statements from a file. I need to convert the sql unloaded froma gupta sqlbase database into sql that SQLServer can understand.
One task is to replace keywords that are not allowed as column names with a compatible name.
In the following code $commands is an array ref that contains sql statements. (There is actually more code here, but I extracted it because it shouldn't be relevant here)
my #KeyWords = ("LEFT", "RIGHT", "PERCENT", "FILE", "PRINT", "CROSS", "PLAN", "TOP", "END", "FILE", "Default", "CHECK", "TEXT");
foreach $cmd (#$commands) {
foreach my $kw (#KeyWords) {
$cmd =~ s/\b$kw\b[^(]/_$kw/gi;
}
push #$converted, $cmd;
}
This works fine for most statements but in the following command "DEFAULT" gets replaced with "_DEFAULT instead of "_DEFAULT". So the second quotation mark is lost.
CREATE TABLE SYSADM.SUBTYPE ( ID_SUBTYPE INTEGER NOT NULL,
ID_TYPE INTEGER NOT NULL,
TYPE VARCHAR(1),
BEZEICH VARCHAR(60),
NUM_COLOR INTEGER,
NUM_TXTCOLOR INTEGER,
"DEFAULT" SMALLINT,
GENER_ARBA SMALLINT,
PROJEKTPLANUNG SMALLINT)
Is there a way to modify the regular expression/substition so this will not remove the second quotation mark? Or an other way?
[^(] matches any single character that is not a left opening paranthesis.
You want to use a negative zero-width lookahead assertion instead:
s/\b$kw\b(?!\()/_$kw/gi;
(Alternatively: (?![(]))
You can also add the replaced character back to the string:
s/\b$kw\b([^(])/_$kw$1/gi;
But note that this will not work in all cases. Especially if there is nothing after the keyword, this pattern will not match whereas zero-width assertion will.
i have string that contains apostrophe and comma's and when i execute insert into SQLite
it gives me error for example with string like this :
...., 'The Smiths - I Know It's Over', .....
"Over": syntax error Unable to execute statement
how can i or what can i do to keep the apostrophe's in the string but preform valid insert?
im using Qt c++ .
You shouldn't be putting arbitrary strings directly into SQL - that's asking for an injection attack. Instead, use bound parameters; something like:
sqlite3_stmt * statement;
sqlite3_prepare(db, "select * from students where name=?", -1, &statement, NULL);
sqlite3_bind_text(statement, 1, "'; drop table students --", -1, SQLITE_STATIC);
sqlite3_step(statement);
sqlite3_finalize(statement);
This will replace the first parameter (?) in the query with the given string, with no danger of any ' character being interpreted as the end of the string, or of any part of the string being executed as SQL.
From the FAQ:
(14) How do I use a string literal that contains an embedded single-quote (') character?
The SQL standard specifies that single-quotes in strings are escaped by putting two single quotes in a row. SQL works like the Pascal programming language in the regard. SQLite follows this standard. Example:
INSERT INTO xyz VALUES('5 O''clock');
I believe the mysql library has a function named mysql_real_escape_string to escape the string. Also, you may use double quotes to surround the string or escape the apostrophe of your input string like this 'The Smiths - I Know It\'s Over',