regular expression like SQL [closed] - asp.net

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a search field form that I use to insert filterexpression on a aspx page.
I would like to force user to insert a string similar to a sql like condition, so it can be:
Start or not with %, so %?
Write letters, numbers, whitespace, underscore but not a special character like %, so [A-Za-z0-9-_+()\s]+
Ends or not with %, so %?$
My reg expression is %?[A-Za-z0-9-_+()\s]+%?$ but it doesn't work because I can write %test%test% that is not a sql like condition.
I have to force writing %test or test% or test or %test% but not %test%test% because the search goes in an error page.
How to do this?
Thanks

You were very close, just missing the start of string operator ^. Without that, you allow a match on your regex on any part of the input, which is why you were able to match things like %test%test%. The expression as you had it would just match starting at the second %.
using System.Text.RegularExpressions;
using System;
public class Program{
public static void Main(string[] args) {
Regex rgx = new Regex(#"^%?[A-Za-z0-9-_+()\s]+%?$");
Console.WriteLine(rgx.IsMatch("%test%")); //True
Console.WriteLine(rgx.IsMatch("%test %test%")); //False
}
}

Related

What's the proper way of Naming a class? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Many people name their classes this way: header-menu
I personally prefer header_logo; as its easier to read as well as copy-paste for a lazy coder.
Is there any significant differences in between naming class with "_" or "-" ?
I am really curious and would love to go with the best method; please enlighten me;
Thanks!
Unfortunately _ is used to separate element or block modifiers. So it is not valid to use with BEM.
Valid names for your example:
header-menu
headerMenu
More related examples:
Hyphen style:
header-menu -> block
header-menu__logo -> element
header-menu_theme_xmas -> block modifier
header-menu__logo_size_big -> element modifier
camelCase style:
headerMenu -> block
headerMenu__logo -> element
headerMenu_theme_xmas -> block modifier
headerMenu__logo_size_big -> element modifier
From the docs:
Naming rules
block-name__elem-name_mod-name_mod-val
Names are written in lowercase Latin letters.
Words are separated by a hyphen (-).
The block name defines the namespace for its elements and modifiers.
The element name is separated from the block name by a double
underscore (__).
The modifier name is separated from the block or element name by a
single underscore (_).
The modifier value is separated from the modifier name by a single
underscore (_).
For boolean modifiers, the value is not included in the name.block-name__elem-name_mod-name_mod-val
More info on naming conventions: https://en.bem.info/methodology/naming-convention/
There is not proper way, just use whatever css name convention you like. Of course, there are some guidelines that you can follow or just ignore like: BEM, SMACSS and many more.

how to exporting data from data table to .csv formate [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to export some data from datatable to .csv file. But the data are not coming to the .csv file its coming as a blank file
A StreamWriter should be closed to have the data written to its internal buffer flushed to the file.
So you really need to enclose the creation of the StreamWriter inside a using statement.
The using statement manages the closing of the file and releases the system resource (the file handle). All this happens automatically when you reach the closing brace at the end of the using block ALSO in case of exceptions.
using(StreamWriter sw = new StreamWriter("D:\\report.csv"))
{
... all the code that read the datatable and write in the stream....
} // here you get: sw.Flush(); sw.Close(); sw.Dispose();
It seems also that you append a comma at the end of each line also for the last column.
Inside the loop the line
if(i != dt.Columns.Count)
is never false because you exit from the loop when the variable i reaches the dt.Columns.Count value. Change it to
if(i < dt.Columns.Count-1)

Regular expression for email in asp.net

There are a lot of question on stack overflow possibly for regular expression for email. I assume I will not get taged as duplicate question...:)
Currently I am using regular expression for email address:
\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*
This works fine with following:
xyz#gmail.com
What I want to allowed here user can give space at begining or at ending and possibly both for instance: " nihantanu#gmail.com " I want to pass it by regular expression Currently it's giving Invalid I believe it should be.
What I tried ^[ \t]+|[ \t]+$ + \w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*
But It's not working I am not more familiar with how to write regular expression. Currently I m trying to learn from Here http://www.regular-expressions.info/examples.html.
Try \s*\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*\s*.

sql statement issue after url rewrite [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am implementing URL REWRITE (IIS 7) in a client's website.
but I have a problem with the sql statement.
before I had this:
select * from 360 where titulo='"&request("titulo")&"'"
and it was ok.
an example of titulo was "Fiexpo Latinoamérica"
my old url was portfolio.asp?titulo=Fiexpo Latinoamérica
but now my new url is /portfolio/fiexpo-latinoamerica
I use ASP replace to remove the spaces and change them with an "-" among others simbols.
so now, my sql statement does not works... because there is no titulo in the database with that name.
how do I do?
I could easyle replace the - with an space, ok but I have "tildes" á é í ó ú
what do I do?????
How is that new URL generated? You should save the new URL (slug) for every item in the database so you can use that to generate the links and to search it when somebody is requesting the page.

Password generator [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to generate some passwords (password generator), but I want to avoid characters that can be confused with each other (for example: Chair2bok). How can I do that?
Why don't you just use a character set that you DO want to use rather than those to avoid?
A standard set of 64 characters
!#%+23456789:=?#ABCDEFGHJKLMNPRS
TUVWXYZabcdefghijkmnopqrstuvwxyz
A larger set of 88 characters
!"#$%&'()*+,-./23456789:;<=>?#ABCDEFGHJKLMNO
PRSTUVWXYZ[\]^_abcdefghijkmnopqrstuvwxyz{|}~
Source: Characters to avoid in automatically generated passwords
I blogged about how to generate a strong password for SQL Server HERE.
It uses strings that you can remove the characters you don't want. I've already removed some like 1, I, 0, and O.
private static string CHARS_LCASE = "abcdefgijkmnopqrstwxyz";
private static string CHARS_UCASE = "ABCDEFGHJKLMNPQRSTWXYZ";
private static string CHARS_NUMERIC = "23456789";
private static string CHARS_SPECIAL = "*-+_%/";
private static string CHARS_ALL = CHARS_LCASE + CHARS_UCASE
+ CHARS_NUMERIC + CHARS_SPECIAL;
To generate passwords of various complexity, you can use the services. For example, GetPas (online password generator).

Resources