|
Well... damn.
I got this one going here
^((\\b[^\\s=]+)=(([^=]|\\\\=)+))*$
which gives me the last key-value pair of the string. I can then chop off the extracted string and run the regex again. Dirty, but does the job.
I will never again mention that Dalek Dave was the poster of the One Millionth Lounge Post, nor that it was complete drivel.
|
|
|
|
|
Yeah...That what I though - no pure regex for you know. Not without extensions...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
Bummer.
But that one got me, evntually gonna write a short article about it...
I will never again mention that Dalek Dave was the poster of the One Millionth Lounge Post, nor that it was complete drivel.
|
|
|
|
|
Beware - it's the second article you promise me! I didn't see the first jet...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
What was the first one again?
I will never again mention that Dalek Dave was the poster of the One Millionth Lounge Post, nor that it was complete drivel.
|
|
|
|
|
Syslog protocol...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
I started the implementation, but I'm a bit busy with my final projects for my apprenticeship.
What I've done so far is a good bit of the message classes, have a look, if you want to...[^]
I will never again mention that Dalek Dave was the poster of the One Millionth Lounge Post, nor that it was complete drivel.
|
|
|
|
|
Assuming that keys can't contain white-space or escaped "=" characters, this should work:
^((?<key>\b[^\s=]+)=(?<value>([^=]|\\=)+))+$
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard,
this one works like a charm, you can't believe how much you helped me
I will never again mention that Dalek Dave was the poster of the One Millionth Lounge Post, nor that it was complete drivel.
|
|
|
|
|
Oh, so the puzzle was here all the time. Well, OG found a solution to you, and I got one too witch OG helped me a little with:
(\w+(?=\=))|((?<!\\)(?<=\=)(.+?)((?=(\s\w+\=))|$))
This sould work in all cases, or so I hope
|
|
|
|
|
Something else to consider... when I've needed to do something like that I did it right-to-left.
You'll never get very far if all you do is follow instructions.
|
|
|
|
|
i want to separate voice and background music from an audio file, for this i have use BSS method. Can any one provide me MATLAB code for BSS(blind source separation)
|
|
|
|
|
And what does this have to do with Regular Expressions[^]?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I can't see how you can't see the connection...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
Yess, I am still struggling with RegEx.
What I have done so far is to validate a string whether it consists of printable ASCII chars only.
string host;
public string Host
{
get { return host; }
set
{
Regex regex = new Regex(@"(\40-\176)");
Match match = regex.Match(value);
if (match.Groups.Count == 1)
{
host = match.Groups[1].Value;
}
}
}
Is there a shorter way of approaching this?
Also, the RegEx currently produces no match if there is an invalid ASCII char in the string, what would I need to do to just remove the faulty part?
Clean-up crew needed, grammar spill... - Nagy Vilmos
|
|
|
|
|
I'm not convinced by your Regex - you're matching the literal string " -~" and capturing it to a group. To indicate a range of characters, you need some square brackets:
([\x20-\x7e])
(I prefer to use hex values, since it's more obvious that they're not decimal values; you don't have to remember that the default is octal.)
Since you just want to remove any characters that aren't in the specified range, the simplest approach is to negate the range and use the Replace method:
set
{
host = Regex.Replace(value, @"[^\x20-\x7e]", string.Empty);
}
However, for a simple case like this, Regex is probably overkill; you could simply loop through the characters, discarding any that you don't want:
set
{
if (!string.IsNullOrEmpty(value))
{
char[] validChars = new char[value.Length];
int validCharIndex = 0;
foreach (char c in value)
{
if ('\x20' <= c && c <= '\x7e')
{
validChars[validCharIndex] = c;
validCharIndex++;
}
}
if (validCharIndex == 0)
{
value = string.Empty;
}
else if (validCharIndex != value.Length)
{
value = new string(validChars, 0, validCharIndex);
}
}
host = value;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard,
thank you very much for your answer. I have developed the Regex a bit further, so it parses only matching strings, causing an InvalidOperationException in case the string contains any invalid characters. This behavior makes sense, since a hostname with removed characters may cause confusion.
I have also added a length maximum of 255 characters, and the string must have at least a single character.
Regex regex = new Regex(@"(^[\40-\176]{1,254}$)");
Match match = regex.Match(value);
if (match.Groups.Count == 2)
{
host = match.Groups[1].Value;
}
else
{
throw new InvalidOperationException(string.Format("The string {0} for host is misformatted. It may only contain printable US-ASCII characters.", value));
}
I am not fully convinced by your idea of iterating through the string - The RegEx makes the code easier to read, and easier to change if someone suddenly requires to allow special characters. In Addition to that, I fear a performance loss when iterating through the string.
Clean-up crew needed, grammar spill... - Nagy Vilmos
|
|
|
|
|
You'd have to test it, but I doubt a Regex is going to be faster than a simple loop through the string.
I'd be inclined to move the length test out of the Regex, and possibly negate the Regex test:
if (value == null)
{
throw new ArgumentNullException("value");
}
if (value.Length < 1 || value.Length > 254)
{
throw new ArgumentException("The host must be between 1 and 254 characters long.", "value");
}
if (Regex.IsMatch(value, "[^\40-\176]"))
{
throw new InvalidOperationException(string.Format("The string {0} for host is misformatted. It may only contain printable US-ASCII characters.", value));
}
host = value;
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
modified 7-Feb-14 8:14am.
|
|
|
|
|
Richard Deeming wrote: I'd be inclined to move the length test out of the Regex,
Why?
Clean-up crew needed, grammar spill... - Nagy Vilmos
|
|
|
|
|
Checking the length of a string with the Length property is almost certainly going to be faster than using a Regex. It also makes the code easier to decipher - you don't have to work out the Regex syntax to see what the length restrictions are - and allows you to generate more granular exceptions, rather than having a single exception if the value doesn't match the Regex.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Gotcha! Thank you very much
Clean-up crew needed, grammar spill... - Nagy Vilmos
|
|
|
|
|
Let me start by saying that I'm not the greatest with Regular Expressions, but from research it seems that they are the best way to go for what I need; I just don't fully understand how to use them with matching groups.
In my application I need to parse a boolean string that can have any number of parameters. The following are some examples:
Name=John or Name=Jane
Name=John or Name=Jane and Date=Now
I need to be parse any variation of these strings ('and' and 'or' are the only boolean operators that can be present) so that I get a list such as:
Name=John
or
Name=Jane
and
Date=Now
I don't need to parse the strings with the '=' sign, but I do need the 'or' & 'and' operators so when I build my query in code I know how to connect each statement to the next (ie:Name=John should be ORed with Name=Jane which should be ANDed with Date=Now) So far I have only been able to get a list such as
Name=John
Name=Jane
Date=Now
by using the following code:
Dim Pattern As String = "(\S+\x3D\S+)"
While Regex.Matches(query, Pattern).Count > 0
Dim oMatches As MatchCollection = Regex.Matches(query, Pattern)
For Each oMatch In oMatches
Dim Operand1 = oMatch.Groups(0).Value
Next
End While
but I lose the boolean operators in the process. If anyone could please help me with a regular expression so I would get the groups I have now, but with the operators in between the appropriate expressions, it would be greatly appreciated.
Thanks.
|
|
|
|
|
I'd suggest doing the matches in phases so that you can track operator presedence: and is higher presendence than or :
In fact you probably don't need to use Regex , String.Split should be able to do the job:
First divide the input based on 'and':
Dim andSeparators() As String = {" and "}
Dim andTerms() as String
andTerms = String.Split(andSeparators, query)
Next for each of the andTerms do a similar String.Split on " or "
|
|
|
|
|
You can use the alternation syntax "(a|b)". A match in your case is an expression in form "parameter=value", or an operator. Operator can be "or" or "and" and must have a space before and after.
Following pattern produces the results you wanted. For an input string "Name=John or Name=Jane and Date=Now" you get matches "Name=John", " or ", "Name=Jane", " and ", "Date=Now":
"(\w+=\w+|\s(or|and)\s)"
If you want to use regex for validation only, you can do it this way (note that you get only a single match with this pattern):
Regex.IsMatch(inputStr, "^(\w+=\w+|\s(or|and)\s)+$");
And you can go even further with validation. Following pattern uses positive look ahead/behind syntax to ensure that operators are enclosed with valid expressions:
Regex.IsMatch(inputStr, "^(\w+=\w+|(?<=\w+=\w+)\s(or|and)\s(?=\w+=\w+))+$");
Gabriel Szabo
modified 8-Nov-13 3:57am.
|
|
|
|
|
Given this input pattern \[.+" and this string w2rddddd["oQookkkkkk"]rrrrrrrrrrr the pattern returns w2rddddd["oQookkkkkk"]rrrrrrrrrrr, that is, up to and including the second double-quote. What I'd like to return is up to and including the first double-quote like w2rddddd["oQookkkkkk"]rrrrrrrrrrr
Clearly my pattern is incorrect but I can't see what I'm doing wrong. Anybody see it?
If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.
|
|
|
|