Hello all I am going through some old code and ran across a reg-ex, I cant figure out what it does, Can anyone shed some light on it.
<(.|\n)*?>|{(.|\n)*?}
it was in a replace string.replace statement.
Put your regex into Regex101.com
At the bottom is a guide titled Your regular expression explained
According to RegexBuddy this is what it dose:
Match either the regular expression below (attempting the next alternative only if this one fails) «<(.|\n)*?>»
Match the character “<” literally «<»
Match the regular expression below and capture its match into backreference number 1 «(.|\n)*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Note: You repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. «*?»
Match either the regular expression below (attempting the next alternative only if this one fails) «.»
Match any single character that is not a line break character «.»
Or match regular expression number 2 below (the entire group fails if this one fails to match) «\n»
Match a line feed character «\n»
Match the character “>” literally «>»
Or match regular expression number 2 below (the entire match attempt fails if this one fails to match) «{(.|\n)*?}»
Match the character “{” literally «{»
Match the regular expression below and capture its match into backreference number 2 «(.|\n)*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Note: You repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. «*?»
Match either the regular expression below (attempting the next alternative only if this one fails) «.»
Match any single character that is not a line break character «.»
Or match regular expression number 2 below (the entire group fails if this one fails to match) «\n»
Match a line feed character «\n»
Match the character “}” literally «}»
The Matches are:
<>
<...>
{}
{...}
when ... is any text
Related
I have the following regular expression, but it only matches the last occurrence of the pattern found. The regular expression is designed to match the following pattern:
A single digit followed by a \
A word followed by \
Another word followed by \
The forth group can either be a single word followed by a \ or 2 or 3 words followed by \
The fifth group must be a floating point number in the format 00.00
The regular expression is:
([0-9])\\(\w+)\\(\w+)\\((\w+\s+\w+\s+\w+)|\w+\s\w+|\w+)\\([+-]?\d*\.\d+)(?![-+0-9\\.])\\
The string being match is:
2\James\Brown\Football Club Mu\15.45\1\Jessie\Ellis\Football Club Performance\15.48\4\Dane\Brown\FC Football \15.52\5\Richardo\Flemmings\Football Club Striders\15.53\7\Lawrence\Brown\Football Club Testing\15.53\8\Jermy\Black\Football Club Ch\15.34\\
The match of the last record is only detected if the regex expression does not terminate with \\ and the string that is to be matched against the regular expression does not terminate with "\\".
Note, the string that is to be compared to the regular expression always terminates with a "\\".
The regex you provided doesn't appear to work at all. I can't make out what you're trying to do with that, especially the '+' and '-' characters. To perfectly match your definition, I've got this:
([0-9])\\\w+\\\w+\\(\w+( \w+)?( \w+)?)\\[0-9][0-9]\.[0-9][0-9]
Although your examples don't quite match your definition, as they have a trailing '\', and the third example has a trailing space in the fourth group. Assuming those examples are valid, I've modified it to this:
([0-9])\\\w+\\\w+\\(\w+( \w+)?( \w+)?) ?\\[0-9][0-9]\.[0-9][0-9]\\
Using regular expressions I want to match a word which
starts with a letter
has english alpahbets
numbers, period(.), hyphen(-), underscore(_)
should not have two or more consecutive periods or hyphens or underscores
can have multiple periods or hyphens or underscore
For example,
flin..stones or flin__stones or flin--stones
are not allowed.
fl_i_stones or fli_st.ones or flin.stones or flinstones
is allowed .
So far My regular expression is ^[a-zA-Z][a-zA-Z\d._-]+$
So My question is how to do it using regular expression
You can use a lookahead and a backreference to solve this. But note that right now you are requiring at least 2 characters. The starting letter and another one (due to the +). You probably want to make that + and * so that the second character class can be repeated 0 or more times:
^(?!.*(.)\1)[a-zA-Z][a-zA-Z\d._-]*$
How does the lookahead work? Firstly, it's a negative lookahead. If the pattern inside finds a match, the lookahead causes the entire pattern to fail and vice-versa. So we can have a pattern inside that matches if we do have two consecutive characters. First, we look for an arbitrary position in the string (.*), then we match single (arbitrary) character (.) and capture it with the parentheses. Hence, that one character goes into capturing group 1. And then we require this capturing group to be followed by itself (referencing it with \1). So the inner pattern will try at every single position in the string (due to backtracking) whether there is a character that is followed by itself. If these two consecutive characters are found, the pattern will fail. If they cannot be found, the engine jumps back to where the lookahead started (the beginning of the string) and continue with matching the actual pattern.
Alternatively you can split this up into two separate checks. One for valid characters and the starting letter:
^[a-zA-Z][a-zA-Z\d._-]*$
And one for the consecutive characters (where you can invert the match result):
(.)\1
This would greatly increase the readability of your code (because it's less obscure than that lookahead) and it would also allow you to detect the actual problem in pattern and return an appropriate and helpful error message.
I've tried with no luck to search the interwebs for a regular expression that checks for:
A positive #
Between 0 and 12
I need to put it in the brackets for this particular line of code
Regex rxValidHeightInches = new Regex();
Any help would be greatly appreciated!
Thanks
This should work ^(\d|(1[0-2]))$
var rxValidHeightInches = new Regex("^(\\d|(1[0-2]))$");
Try this one.
(?<!-)(\b((1[01])|[1-9])\b)
It matches 1 - 9 or 10 or 11. Negative numbers are always excluded.
Explaination
-Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind) «(?<!-)»
---Match the character “-” literally «-»
-Match the regular expression below and capture its match into backreference number 1 «(\b((1[01])|[1-9])\b)»
---Assert position at a word boundary «\b»
---Match the regular expression below and capture its match into backreference number 2 «((1[01])|[1-9])»
------Match either the regular expression below (attempting the next alternative only if this one fails) «(1[01])»
---------Match the regular expression below and capture its match into backreference number 3 «(1[01])»
------------Match the character “1” literally «1»
------------Match a single character present in the list “01” «[01]»
------Or match regular expression number 2 below (the entire group fails if this one fails to match) «[1-9]»
---------Match a single character in the range between “1” and “9” «[1-9]»
---Assert position at a word boundary «\b»
SCREENSHOT
This one includes 0 - 12
(?<!-)(\b((1[0-2])|[0-9])\b)
Consider following string
"Some" string with "quotes" and \"pre-slashed\" quotes
Using regex, I want to find all the double quotes with no slash before them. So I want the regex to find four matches for the example sentence
This....
[^\\]"
...would find only three of them. I suppose that's because of the regex's state machine which is first validating the command to negate the presence of the slash.
That means I need to write a regex with some kind of look-behind, but I don't know how to work with these lookaheads and lookbehinds...im not even sure that's what I'm looking for.
The following attempt returns 6, not 4 matches...
"(?<!\\)
"(?<!\\")
Is what you're looking for
If you want to match "Some" and "quotes", then
(?<!\\")(?!\\")"[a-zA-Z0-9]*"
will do
Explanation:
(?<!\\") - Negative lookbehind. Specifies a group that can not match before your main expression
(?!\\") - Negative lookahead. Specifies a group that can not match after your main expression
"[a-zA-Z0-9]*" - String to match between regular quotes
Which means - match anything that doesn't come with \" before and \" after, but is contained inside double quotes
You almost got it, move the quote after the lookbehind, like:
(?<!\\)"
Also be ware of cases like
"escaped" backslash \\"string\"
You can use an expression like this to handle those:
(?<!\\)(?:\\\\)*"
Try this
(?<!\\)(?<qs>"[^"]+")
Explanation
<!--
(?<!\\)(?<qs>"[^"]+")
Options: case insensitive
Assert that it is impossible to match the regex below with the match ending at this position (negative lookbehind) «(?<!\\)»
Match the character “\” literally «\\»
Match the regular expression below and capture its match into backreference with name “qs” «(?<qs>"[^"]+")»
Match the character “"” literally «"»
Match any character that is NOT a “"” «[^"]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “"” literally «"»
-->
code
try {
if (Regex.IsMatch(subjectString, #"(?<!\\)(?<qs>""[^""]+"")", RegexOptions.IgnoreCase)) {
// Successful match
} else {
// Match attempt failed
}
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
I'm working on pl/sql code where i want to replace ';' which is commented with '~'.
e.g.
If i have a code as:
--comment 1 with;
select id from t_id;
--comment 2 with ;
select name from t_id;
/*comment 3
with ;*/
Then i want my result text as:
--comment 1 with~
select id from t_id;
--comment 2 with ~
select name from t_id;
/*comment 3
with ~*/
Can it be done using regex in C#?
Regular expression:
((?:--|/\*)[^~]*)~(\*/)?
C# code to use it:
string code = "all that text of yours";
Regex regex = new Regex(#"((?:--|/\*)[^~]*)~(\*/)?", RegexOptions.Multiline);
result = regex.Replace(code, "$1;$2");
Not tested with C#, but the regular expression and the replacement works in RegexBuddy with your text =)
Note: I am not a very brilliant regular expression writer, so it could probably have been written better. But it works. And handles both your cases with one-liner-comments starting with -- and also the multiline ones with /* */
Edit: Read your comment to the other answer, so removed the ^ anchor, so that it takes care of comments not starting on a new line as well.
Edit 2: Figured it could be simplified a bit. Also found it works fine without the ending $ anchor as well.
Explanation:
// ((?:--|/\*)[^~]*)~(\*/)?
//
// Options: ^ and $ match at line breaks
//
// Match the regular expression below and capture its match into backreference number 1 «((?:--|/\*)[^~]*)»
// Match the regular expression below «(?:--|/\*)»
// Match either the regular expression below (attempting the next alternative only if this one fails) «--»
// Match the characters “--” literally «--»
// Or match regular expression number 2 below (the entire group fails if this one fails to match) «/\*»
// Match the character “/” literally «/»
// Match the character “*” literally «\*»
// Match any character that is NOT a “~” «[^~]*»
// Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
// Match the character “~” literally «~»
// Match the regular expression below and capture its match into backreference number 2 «(\*/)?»
// Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
// Match the character “*” literally «\*»
// Match the character “/” literally «/»
A regex is not really needed - you can iterate on lines, locate the lines starting with "--" and replace ";" with "~" on them.
String.StartsWith("--") - Determines whether the beginning of an instance of String matches a specified string.
String.Replace(";", "~") - Returns a new string in which all occurrences of a specified Unicode character or String in this instance are replaced with another specified Unicode character or String.