How to drop some characters with Regex in vs? - c#

I have a sql table and I have a column which includes data such as: B757-34-11-00-I-1, A300-223100-0503-1 etc.
A300-223100-0503-1 -> 223100-0503-1
B757-34-11-00-I-1 -> 34-11-00-I-1
How can i do that with regex? I need two kinds of solutions: sql and C#. how can I do that with sql query in SQL and C#
i need drop charater as far as "-" may be dropping more than 5 characters or less than? i need also drop "-"

A regular expression is overkill for simple string manipulation like this.
C#:
value.Substring(value.indexOf('-') + 1)
SQL:
substring(field, charindex('-', field) + 1, 1000)
(The last parameter could be calculated as len(field) - charindex('-', field) - 1, but you can just use a value that you know is larger than the max length.)

Is it always just dropping the first 5 characters? If so, you don't need to use a regex at all.
C#:
value = value.Substring(5);
T-SQL:
SELECT SUBSTRING(field, 5, LEN(field) - 5)

In SQL:
SUBSTRING(col, PATINDEX('%-%', col) + 1)
in C#:
val.Substring(val.IndexOf('-') + 1)
This requirement is so simple, there is no need for regexes (and SQL Server does not natively support them anyway if you do not add this via a stored procedure implemented in .net).

string input = "A300-223100-0503-1";
Regex rgx = new Regex("^[^-]+-(.*)$");
string result = rgx.Replace(input, "$1");
Console.WriteLine(result);

Related

Replacing single quotes in full sql queries with C#

In our C# desktop-application we generate a lot of dynamic sql-queries. Now we have some troubles with single quotes in strings. Here's a sample:
INSERT INTO Addresses (CompanyName) VALUES ('Thomas' Imbiss')
My question is: How can I find and replace all single quotes between 2 other single quotes in a string? Unfortunately I can't replace the single quotes when creating the different queries. I can only do that after the full query is created and right before the query gets executed.
I tried this pattern (Regular Expressions): "\w\'\w"
But this pattern doesn't work, because after "s'" there's a space instead of a char.
I am sorry to say, there is no solution in approach you expect.
For example, have these columns and values:
column A, value ,A',
column B, value ,B',
If they are together in column list, you have ',A',',',B','.
Now, where is the boundary between first and second value? It is ambiguous.
You must take action when creating text fields for SQL. Either use SQL parameters or properly escape qoutes and other problematic characters there.
Consider showing the above ambiguous example to managers, pushing the whole problem back as algorithmically unsolvable at your end. Or offer implementing a guess-work and ask them whether they will be happy if content of several text fields can get mixed in some cases like above one.
At time of SQL query creation, if they do not want to start using SQL parameters, the solution for enquoting any input string is as simple as replacing:
string Enquote(string input)
{
return input.All(c => Strings.AscW(c) < 128) ? "'" : "N'"
+ input.Replace("'", "''")
+ "'"
}
Of course, it can have problem with deliberately malformed Unicode strings (surrogate pairs to hide ') but it is not normally possible to produce these strings through the user interface. Generally this can be still faster than converting all queries to versions with SQL parameters.

Regex to replace part of a query string with literals

I have a slight problem in that I am passing several SQL Server Like queries (which I don't have control over) in my .Net application and I need to escape some of the characters to make them literals for the database.
For example, suppose the field I am trying to query looks something like:
This_is [a] long_text field with [literal] characters in it
And my query looks something like this:
SELECT * FROM [MyTable] where [MyField] Like 'This_% [literal]%'
Now, this should get that data, the challenge is that the _ and [ characters are not seen by SQL Server as the literal characters, so I have to update my query to:
SELECT * FROM [MyTable] where [MyField] Like 'This[_]% [[]literal]%'
And then it works.
So, my question is that I would like to write a VB.Net function that uses RegEx to find the Like part of the statement and then to replace the characters.
Something along the lines of:
Public Function ConvertQuery(strSQL as string)
... Some kind of Regex that searches for the word "Like"
and then takes everytihng between the 2 "'" characters
Then replaces the "_" with "[_]" and "[" with "[[]"
Return ChangedString
End Function
I know I am giving REALLY bad details, I know what i want, I just can't figure out how to do it with RegEx.
ALSO, if there is a smarter way to do this with SQL Server Like statments, please also let me know!!!
Thanks!!
Seeing that C# tag, I assume you are OK with an answer in C#.
string input = "This_is [a] long_text field with [literal] characters in it";
var output = Regex.Replace(input, #"[_\[]","[$0]");
EDIT
string input = "SELECT * FROM [MyTable] where [MyField] Like 'This_% [literal]'";
var output = Regex.Replace(input, #"(?<=Like[ ]*\'.+?)[_\[]", "[$0]");

Check if two Strings contain the same pattern

my application is going to need a string matching function between an inputed string and a string stored in my DB. Those strings concerns product dimensions (like length, heigh etc.) My goal is to check if the string inputed already exists in my database but maybe with another pattern , for example :
input : "vis 4*40" should return true if I have something like "vis 4X40" or "vis 4 x 40" or "vis 4* 40" in my database.
The only way i've found yet is to normalize my databse using a regexp to replace pattern like :
\d+ *[xX*] *\d+
with a normalized one (for example NumberXNumber), do the same with the string in input then try to find it. However I was wondering if any tool already existed and would allow me to do that. (I'm working in C# .NET V4)
A better way would be to concatenate both the data and then match them
string target=inputString+"#"+storedString;
if(Regex.IsMatch(target,"(\d+) *[xX*] *(\d+)#\1 *([xX*]) *\2"))
//the string is in the database
else
//it is not
Just use the user-provided wildcard, replace the * with % and query your table: SELECT col FROM table WHERE col LIKE 'vis 4%40'
Please make sure you execute your queries with parameters.

Regex to extract function-name, & it's parameters

I am building an application where the user can specify an expression for some fields. The expression shall contain functions too. I need to evaluate these expressions and display final value in report.
I had an expression to extract function-name & it's paramters. Previously, the function parameters were decimal values. But now, the parameters can also be expression.
For ex,
Round( 1 * (1+ 1 /100) % (2 -1), 0)
Function-name : Round
Parameter1 : 1 * (1+ 1 /100) % (2 -1)
Parameter2 : 0
Previous Regex:
string pattern2 = #"([a-zA-Z]{1,})[[:blank:]]{0,}\(([^\(\)]{0,})\)";
This regex does not help me anymore to find expression-parameters.
Can someone help me with the right regex to extract function-name & parameters? I am implement all or most of the functions supported by Math class.
The program is built in c#
Thanks in advance for the help.
"^\s*(\w+)\s*\((.*)\)"
group(1) is function name
split group(2) with "," you get para list.
updated
since I don't have Windows system (.Net either), I test it with python. nested function is not a problem. if we add "^\s*" at the beginning of the expression:
import re
s="Round(floor(1300 + 0.234 - 1.765), 1)"
m=re.match("^\s*(\w+)\s*\((.*)\)",s)
m.group(1)
Output: 'Round'
m.group(2)
Output: 'floor(1300 + 0.234 - 1.765), 1'
you can split if you like:
m.group(2).split(',')[0]
Out: 'floor(1300 + 0.234 - 1.765)'
m.group(2).split(',')[1]
Out: ' 1'
well, if your function nesting is like f(a(b,c(x,y)),foo, m(j,k(n,o(i,u))) ), my code won't work.
You could try writing a parser, instead of using regular expressions.
The Irony library is (in my opinion) extremely easy to use, and in the examples there's something very similar to what you are trying to do.
From the post Regex for matching Functions and Capturing their Arguments, you can extract your function using Kent regex and use this code to extract parameters form the last group :
string extractArgsRegex = #"(?:[^,()]+((?:\((?>[^()]+|\((?<open>)|\)(?<-open>))*\)))*)+";
var ArgsList = Regex.Matches(m.Groups[m.Groups.Count - 1].Value, extractArgsRegex);

Extract substring from string with Regex

Imagine that users are inserting strings in several computers.
On one computer, the pattern in the configuration will extract some characters of that string, lets say position 4 to 5.
On another computer, the extract pattern will return other characters, for instance, last 3 positions of the string.
These configurations (the Regex patterns) are different for each computer, and should be available for change by the administrator, without having to change the source code.
Some examples:
Original_String Return_Value
User1 - abcd78defg123 78
User2 - abcd78defg123 78g1
User3 - mm127788abcd 12
User4 - 123456pp12asd ppsd
Can it be done with Regex?
Thanks.
Why do you want to use regex for this? What is wrong with:
string foo = s.Substring(4,2);
string bar = s.Substring(s.Length-3,3);
(you can wrap those up to do a bit of bounds-checking on the length easily enough)
If you really want, you could wrap it up in a Func<string,string> to put somewhere - not sure I'd bother, though:
Func<string, string> get4and5 = s => s.Substring(4, 2);
Func<string,string> getLast3 = s => s.Substring(s.Length - 3, 3);
string value = "abcd78defg123";
string foo = getLast3(value);
string bar = get4and5(value);
If you really want to use regex:
^...(..)
And:
.*(...)$
To have a regex capture values for further use you typically use (), depending on the regex compiler it might be () or for microsoft MSVC I think it's []
Example
User4 - 123456pp12asd ppsd
is most interesting in that you have here 2 seperate capture areas. Is there some default rule on how to join them together, or would you then want to be able to specify how to make the result?
Perhaps something like
r/......(..)...(..)/\1\2/ for ppsd
r/......(..)...(..)/\2-\1/ for sd-pp
do you want to run a regex to get the captures and handle them yourself, or do you want to run more advanced manipulation commands?
I'm not sure what you are hoping to get by using RegEx. RegEx is used for pattern matching. If you want to extract based on position, just use substring.
It seems to me that Regex really isn't the solution here. To return a section of a string beginning at position pos (starting at 0) and of length length, you simply call the Substring function as such:
string section = str.Substring(pos, length)
Grouping. You could match on /^.{3}(.{2})/ and then look at group $1 for example.
The question is why? Normal string handling i.e. actual substring methods are going to be faster and clearer in intent.

Categories