I neeed a c# regex for this 2 cases.
1)MyConstantText
2)MyConstantText.[a-zA-Z]
ex.
My const text is Hello, then regex must match
Hello
Hello.ashdkajshd
Do not forget to escape when creating regular expressions:
String text = "Hello";
// Escape text as well as dot (\.)
// Technically, you do want to escape "Hello", but since
// text can be an arbitrary string, you'd better do it
String pattern = Regex.Escape(text) + #"(\.[a-zA-Z]+)?";
// Simple test
Console.Write(Regex.Match("Hello.ashdkajshd", pattern).Value);
Remark: Please note, that pattern, provided in the question (MyConstantText.[a-zA-Z]) doesn't match the sample in the question ("Hello.ashdkajshd") but "Hello.a" only. So, I've change the corresponding subpattern into [a-zA-Z]+ (note +).
Here is tuto for regex in c# ... if you got an error you can post it
Related
I'm trying to find a regexp that only matches strings if they don't contain a dot, e.g. it matches stackoverflow, 42abc47 or a-bc-31_4 but doesn't match: .swp, stackoverflow or test..
^[^.]*$
or
^[^.]+$
Depending on whether you want to match empty string. Some applications may implicitly supply the ^ and $, in which case they'd be unnecessary. For example: the HTML5 input element's pattern attribute.
You can find a lot more great information on the regular-expressions.info site.
Use a regex that doesn't have any dots:
^[^.]*$
That is zero or more characters that are not dots in the whole string. Some regex libraries I have used in the past had ways of getting an exact match. In that case you don't need the ^ and $. Having a language in your question would help.
By the way, you don't have to use a regex. In java you could say:
!someString.contains(".");
Validation Require: First Character must be Letter and then Dot '.' is not allowed in Target String.
// The input string we are using
string input = "1A_aaA";
// The regular expression we use to match
Regex r1 = new Regex("^[A-Za-z][^.]*$"); //[\t\0x0020] tab and spaces.
// Match the input and write results
Match match = r1.Match(input);
if (match.Success)
{
Console.WriteLine("Valid: {0}", match.Value);
}
else
{
Console.WriteLine("Not Match");
}
my current string regex is working but I wanted to add <?php and <?PHP. Here is the code:
#"\b(public|private)\b"
string Keywords = #"\b(public|private)\b";
This is how you can do a div
#"<\s*div[^>]*>(.*?)<\s*/div\s*>";
How can I add <?php and <?PHP in the same regex?
To specify a set of acceptable characters in your pattern, you can either build a character class yourself or use a predefined one. A character class lets you represent a bunch of characters as a single item in a regular expression. You can build your own character class by enclosing the acceptable characters in square brackets.
Read Using Regular Expressions with PHP for more details and learn how to use php in regex.
Hope it helps.
<?php is not a word so you cannot use \b to match it
var regex = new Regex(#"\b(public|private)\b|<\?php", RegexOptions.IgnoreCase);
var match = regex.Match("public");
Console.WriteLine(match.Value);
match = regex.Match("<?php");
Console.WriteLine(match.Value);
Console.ReadLine();
I'm a real regex n00b so I ask your help:
I need a regex witch match only letters and numbers and exclude punctations, non ascii characters and spaces.
"ilikestackoverflow2012" would be a valid string.
"f### you °§è" not valid.
"hello world" not valid
"hello-world" and "*hello_world*" not valid
and so on.
I need it to make a possibly complex business name url friendly.
Thanks in advance!
You don't need regex for this.
string s = "......"
var isValid = s.All(Char.IsLetterOrDigit);
-
I need it to make a possibly complex business name url friendly
You can also use HttpUtility.UrlEncode
var urlFriendlyString = HttpUtility.UrlEncode(yourString);
To validate a string you can use the following regular expression with Regex.IsMatch:
"^[0-9A-Za-z]+$"
Explanation:
^ is a start of string anchor.
[...] is a character class.
+ means one or more.
$ is an end of string anchor.
I need it to make a possibly complex business name url friendly
Then you want to replace the characters that don't match. Use Regex.Replace with the following regular expression:
"[^0-9A-Za-z]+"
Explanation:
[^...] is a negated character class.
Code:
string result = Regex.Replace(input, "[^0-9A-Za-z]+" , "");
See it working online: ideone
Note that different business names could give the same resulting string. For example, businesses whose names contain only Chinese characters will all give the empty string.
You can use below regex.
^[a-zA-Z0-9]+$
^[0-9a-zA-Z]+$
Matches one or more alphanumeric characters with no spaces or non-alpha characters.
Try this:
var regex = new Regex(#"^[a-zA-Z0-9]+$");
var test = new[] {"ilikestack", "hello world", "hello-world", "###"};
foreach (var s in test)
Console.WriteLine("{0}: {1}", s, regex.IsMatch(s));
EDIT: If you want something like #Andre_Miller said, you should use the same regex with Regex.Replace();
Regex.Replace(s, #"[^a-zA-Z0-9]+", "")
OR
var regex = new Regex(#"^[a-zA-Z0-9]+$");
regex.Replace("input-string-##$##");
Try
^[a-zA-Z0-9]+$
www.regexr.com is a GREAT resource.
What's wrong with [:alnum:]? It's a posix standard. So your whole regex would be: ^[:alnum:]+$.
The wikipedia article for regular expressions includes lots of examples and details.
I have a string like 30+20%. Now I want to replace 20% with (20/100). Thats it.
If the percent doesn't occur in any other situation in the string, you don't even need a regular expression:
s = s.Replace("%", "/100");
To add the parentheses you need the regular expression though:
s = Regex.Replace(s, #"(\d{1,3})%", "($1/100)");
string s="30+20%";
s=s.Replace("%","/100)");
s=s.Replace("+","+(");
I'll just assume you run Perl
input="30+20%"
echo $input | perl -pe 's#(\d+)%#\($1/100\)#g'
EDIT: just read the tags, anyways, the regex should work in C#
That should be an easy regex to try. 1 to 3 digits followed by a percentage sign.
You need to capture the 1-3 digits group for backreference, and use it to create
(DIGITS/100) string.
You can play here :http://gskinner.com/RegExr/ to learn regexes.
I'm not sure what programming language are you using but this is how you would do this in python:
import re
re.sub(r'(\d*)%', r'\1/100', '30+20%')
The returned string will be '30+20/100'.
Explanation:
Let's look at the regex. r'\d*%' is a regex that matches a series of digits followed by the % sign. I put paranthesis arount (\d*) to tell the regex compiler that the series of digits (aka the number) is the first group. The second arguemnt tells the sub functions how to replace the matched string. The argument '\1/100' tells the sub function I want it to replace the matched string with the value of the first group matched by the regex (through the \1 part) followed by /100.
You can check the python re module for more information.
Try this
string resultString = null;
try {
resultString = Regex.Replace(subjectString, #"\b(\d+)%", "($1/100)");
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
See this java code :-
s = s.replaceAll( "\\\\", "\\\\\\\\" ).replaceAll( "\\$", "\\\\\\$" );
I sorta don't understand it. It's a regex replace all.
I've tried the following C# code...
text = text.RegexReplace("\\\\", "\\\\\\\\");
text = text.RegexReplace("\\$", "\\\\\\$");
But if i have the following unit test :-
} ul[id$=foo] label:hover {
The java code returns: } ul[id\$=foo] label:hover {
My c# code returns: } ul[id\\\$=foo] label:hover {
So i'm not sure I understand why my c# code is putting more \'s in, mainly with regards to how these control characters are being represented.. ??
Update:
So, when i use XXX's idea of just using text.Replace(..), this works.
eg.
text = text.Replace("\\\\", "\\\\\\\\");
text = text.Replace("\\$", "\\\\\\$");
But I was hoping to stick with RegEx... to try and keep it as close to the java code as possible.
The extension method being used is...
public static string RegexReplace(this string input,
string pattern,
string replacement)
{
return Regex.Replace(input, pattern, replacement);
}
hmm...
Java needs all $ signs escaped in its replace string - "\\\\\\$" means \\ and \$. Without it it throws an error: http://www.regular-expressions.info/refreplace.html (look for "$ (unescaped dollar as literal text)").
Remember $1, $0 etc are replaced the text with captured groups, so there are a part of the syntax on the second argument to replaceAll. C# has a slightly different syntax, and doesn't require the extra slash, which it takes literally.
You could write:
text = text.RegexReplace(#"\\", #"\\");
text = text.RegexReplace(#"\$", #"\$");
Or,
text = text.RegexReplace(#"[$\\]", #"\$&");
I think it's the equivalent of this C# code:
text = text.Replace(#"\", #"\\");
text = text.Replace("$", #"\$");
The # indicates a verbatim string in C#, meaning that the backslashes in strings don't have to be escaped with more backslashes. In other words, the code replaces a single backslash with a double backslash and then replaces a dollarsign with a backslash followed by a dollarsign.
If you were to use the regex function, it would be something like this:
text = text.RegexReplace(#"\\", #"\\");
text = text.RegexReplace(#"\$", #"\$$");
Note that in the regex pattern (the first parameter), backslashes are special, while in the replacement (the second parameter) it is the dollarsigns that are special.
The code quotes the backslashes and '$' characters in the original string.
Java regex parsing: http://download.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html
C#: http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx
I think that in Java, you have to escape the \ character by using \, but in C#, you don't. Try taking out half of the \ in your C# version.