|
ohkay.bro thanks.
and how can i contact you here on code project?
is there any message system here?
|
|
|
|
|
You can leave me a message on my messaged board on my profile page.
Cheers!
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
Same negative trick works, doesn't it?
(?!\.[^.]*\.)(?!\-[^-]*\-)(?!\_[^_]*\_)
Or (?![.-_][^.-_]*[.-_]) if he wants at most one of any of the three.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Just a quick thought. Why not handle repeated . - _ with negatives? Something like (?!\.\.)(?!\-\-)(?!\_\_) or (?!\.\.|\-\-|\_\_)
I've had this idea kicking around the back of my heads since another member posted somewhere that you can't do [a similar thing] with regex.
Otherwise an excellent answer and +5
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
I'm trying to write a scanner using Flex.
The problem is that one of my regex's works in the Visual Studio Find dialog, but not in Flex when tested on the same file!
By "not working" I mean that it doesn't find any matches.
\'((.)|(\\['"?\\abfnrtv])|(\\[0-7]{1,3})|(\\[Xx][0-9a-fA-F]+))\'
This regex is supposed to recognize any character literals, like 'a' , '\t' , '\012' , or '\x01AF' .
Can anyone suggest why this regex is not finding any matches in Flex?
SOLVED: It turns out that I had other rules that were consuming the character literals earlier in the process.
The difficult we do right away...
...the impossible takes slightly longer.
modified 6-Sep-12 16:11pm.
|
|
|
|
|
Taking a quick look at the (not terribly impressive) Flex docco, I'd suggest you try the Posix-compliance switch to Flex to see if that helps. Either way, you'll have eliminated some variables.
fwiw, Expresso thinks you regex is OK.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Peter_in_2780 wrote: Expresso thinks you regex is OK.
Thanks for your help.
I solved it.
It turns out to be a case of the computer doing what I told it to do instead of what I wanted it to do.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Hi,
I have file lectures and I would like to change the nameing convention.
Currently, it has the format of {detail -} subject - author.wmv
I would like to rename with the nameing convention of author - subject { - detail}.wmv
Some observation, initialy, the author is at the end of the file name preceded by a seperator of minus sign.
{detail -} is optional and thus some file name utilize. Thus, we can have more than one minus sign as seperator but author name is always at end of file name preceded by the minus sign seperator.
Example file names;
e4 e5 - The Four Kn1ghts for Black - Vig0rito.wmv
e4 e5 The Sc0tch for Black - Part 1 - Vig0rito.wmv
2+1=2; A Practical Endgam3 Example from L1nares - P@schall.wmv
Result ( I would like. That is, lecturer seperator topic )
Vig0rito - e4 e5 - The Four Kn1ghts for Black.wmv
Vig0rito - e4 e5 The Sc0tch for Black - Part 1.wmv
P@schall - 2+1=2; A Practical Endgam3 Example from L1nares.wmv
Purpose, I would like to access material by author.
Thank in advance for your help.
|
|
|
|
|
You can use System.IO.Path.GetFileNameWithoutExtension() . Then find the position of the last "-" with the LastIndexOf('-') function of the string class. Build up your new string from the respective Substring() s.
|
|
|
|
|
Thank you for your reply Bernhard and introduction to system io.
Currently, I am using a free file renaming tool that utilize regexp. After looking at the coding and re-familiarizing myself with regexp and having no luck, I made a post. The next morning, I came with a quasi-solution. I do not want to spam the tool but it is the most complex rfr*k according to review.
What unique about this file title renaming is the number of seperators can vary. Thus, mp3 titles with challenging non-fix number of seperators would find this thread informative. I am stump trying to access the last seperator with $ anchor to create an expression and thus sent this sos.
Here is my quasi-solution and hope it helps some with similar problem.
(?x)(.*?)[ ]*?[-]+[ ]*?(.*) [ ]*?[-]+[ ]*?(.*) # start of swap 2 fields \3 zzzz \2 zzzz \1
(?x)[ ]*[-][ ] # Handle Linaries example _
(?x)(.*?)[ ]*?[_]+[ ]*?(.*)$ # Linaries example \2 zzzz \1
\bzzzz\b # Convert back to minus sign -
(.?)\bwmv\b # remove the .wmv after the author name to null
I use zzzz and _ as temporary strings with the understanding that they are unique in my files. Adapt accordingly. I adapted from a swap 2 fields expression. The five lines and replace work for my demo example. Thanks.
Edit: Some minor quirks but liveable. tx
modified 6-Aug-12 23:05pm.
|
|
|
|
|
OK, that's different background.
In that case, I'd rely on the "greedy" character of wild cards.
(?<part1>.*)\s-\s(?<part2>.*)(?<ext>\.wmv) will catch the parts and the extension,
and the re-order it to become part2 - part1.ext
|
|
|
|
|
To cut down the detail, here is my input:
[char](15)
[varchar](23)
So the first field is a square bracket '[', a string and a close bracket ']'. Next is a regular bracket '(', a number, and a close bracket ')'.
I want to use Emacs to replace so here is the result:
15 char
23 varchar
So take out all those bracket or square bracket. Number goes first, a space and the string.
How to do it? I know how to swap values, but adding the pattern of string, I don't know.
|
|
|
|
|
Use (automatically) numbered capture groups:
Matching regex
\[(\S*)\]\((\d*)\)
will capture a string of non-whitepace between [ and ] into $1 and the following number in ( ) into $2.
Your replacement string is then
$2 $1
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Sorry Peter, does not work in Emacs 23.3.1 Windows version.
When typing in the search string, Emacs does not complain, so it seems like working.
But it just does not do the replace.
|
|
|
|
|
I'm not familiar with Emacs (in the last 25 years, anyway). It seems to use a very different regex engine from the ones we meet here (in .NET languages, PHP, Java and so on). You might do better to ask in an Emacs forum.
Sorry I can't help any more.
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
|
Well I find out the result myself using sed! Well with the help from using sed. After some search, I find out I can do the following:
1. Find bracket [ or ]
- I need to escape this in search string (\[ or \])
- To search for [abc] -> \[\([a-z]*\)\]
2. Find parentheses ( or )
- I do not escape this in search string
- To search for (123) -> (\([0-9]*\))
3. Use simple [a-z] or [0-9] instead of \S, \w, \d
Therefore I have:
echo [abc](123) | sed -e "s/\[\([a-z]*\)\](\([0-9]*\))/\2 \1/"
Output:
123 abc
So in emacs, I do the following:
1. M-x regular-regexp
2. \[\([a-z]*\)\](\([0-9]*\))
3. \2 \1
(Make sure there is a space between \2 and \1.)
modified 5-Aug-12 23:32pm.
|
|
|
|
|
Good one. Now we all know a bit more about a different regex engine (Emacs/sed).
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
I have this string:
var Line = "Dev: 0 Model: TOSHIBA MK3265GSX Serial: 20FDF20WS FW: GJ002H STW: 0 MaxLBA: 625142447 FDESUPPORTED: 0 PREBOOT: 0 DRIVETRUSTENABLED: 0 DRIVETRUSTSUPPORTED: 0 w128: 41 FULLFW: GJ002H SERVOFW: SDLSUPPORTED: 1 PLATFORM: 0 SAFE: 1 DSTTIMEOUT: 103 ISBOOTORSYSTEM: 1";
I am trying to use RegEx to extract all Key/Value pairs. So far I have:
private static string getWord(ref string Line, string WordToFind)
{
var retVal = string.Empty;
if (Line.Contains(WordToFind))
{
var expression = @"(?:(?'Key'\S+): (?'Value'.*?)) ";
Match match = Regex.Match(Line, expression,RegexOptions.IgnoreCase);
if (match.Success)
{
string key = match.Groups[1].ToString();
}
}
return retVal;
}
When I pass in "Model:" I get back "Dev". What am I doing wrong here?
If it's not broken, fix it until it is
|
|
|
|
|
Is this C#? I've not seen that method of creating named capture groups. Though, if you are going to name your capture groups, you may as well retrieve them by name rather than by index.
Also, you only check if the line contains the string to find. Nowhere in your code do you actually look for that as a key or value.
And consider that regular expressions are greedy. Your "Value" capture group will capture everything after the first ": ".
Also, according to your regular expression, you can have an empty value ("*" means zero or more). I suspect that is not what you want, but maybe you do.
Finally, I'm not really sure how "SERVOFW: SDLSUPPORTED: 1" fits into the key/value scenario. Maybe that's a typo.
Based on the assumption that values can have spaces, but keys can't have spaces, this may be a more appropriate regular expression:
(?<KEY>(?!:| ).)+: (?<VALUE>((?!:).)+)(?= (((?!:| ).)+:)|$)
Keep in mind that this will find all matches, so you don't watch to just find the first match as you are doing in your example. You want to loop through them all and compare the key/value against the word you are looking for.
modified 19-Jun-12 18:41pm.
|
|
|
|
|
AspDotNetDev wrote: I'm not really sure how "SERVOFW: SDLSUPPORTED: 1" fits into the key/value scenario
Now that I think about it, "SERVOFW" is probably a key with an empty value. That'd take a bit more time to figure out. I'll leave that as an exercise for the reader.
|
|
|
|
|
I'm piecing toget code formv various places becuase it all looks like greek to me. I'm fairly frustrated at this point. This shouldn't be this difficult.
Anyway, here's my code:
if (Line.Contains(WordToFind))
{
Regex r = new Regex(
"(?:(?'Key'\\S+): (?'Value'.*?))",
RegexOptions.RightToLeft
| RegexOptions.CultureInvariant
| RegexOptions.Compiled
);
Match m = r.Match(WordToFind);
if (m.Success)
{
}
}
I get no matches for the word "Model".
Also, once I get a match, how do I get the Key/Value data???
If it's not broken, fix it until it is
|
|
|
|
|
Why did you edit your response? The code I see in the email notification I was sent for your reply to my message looks more correct than the code I now see.
For one, you're going to need a for loop to iterate over the result of Matches (not Match). Match finds a single result, and Matches finds all results.
For two, r.Match(WordToFind) is searching "WordToFind" rather than "Line".
Kevin Marois wrote: once I get a match, how do I get the Key/Value data
You can get groups based on the name. Something like this (I don't have my compiler open, so it may vary slightly):
String key = m.Groups["Key"].Value;
String value = m.Groups["Value"].Value;
Kevin Marois wrote: RegexOptions.RightToLeft
This may drastically change the functionality, but I'm not exactly sure what it does, so you may want to Google this.
Kevin Marois wrote: I'm fairly frustrated at this point. This shouldn't be this difficult.
Regular expressions are complicated, but powerful. You'll get used to them over time. From what others say, Expresso is a good tool to learn regular expressions, though I use a custom tool I built for myself.
|
|
|
|
|
pretty new to regex. this is a great article.
can someone tell me whats wrong with my expression? I'm using this from C#. I'm getting the response from a blog in a malformed xml format. Need to extract entries out of it. In a simple format inpu is similar to the following.
string input = @"<entry><id>tag:myblog.com try</entry><entry><id>tag:myblog.com tryagain</entry><entry><id>tag:myblog.com hello </enty>";
I need to identify the number of entries, and then processing each of them.
Regex blogsRegEx = new Regex(@"<entry><id>tag:myblog.*</entry>");
MatchCollection blogEntries = blogsRegEx.Matches(input);
I always get just 1 entry. It matches the whole thing instead of matching multiple strings in the pattern <entry<id>tag:myblog....</entry>.
Can someone help what am I missing here? do i need to use subexpressions here?
modified 29-Apr-12 19:28pm.
|
|
|
|
|
Not sure about the regex (It makes my brain hurt), but you can use linq instead
string findText = @"<entry><id>tag:myblog";
int entriesCount = blogsText.Count(t => t.equals(findText));
Have not tested it but it should give you the correct results by just tweaking your findText variable.
|
|
|
|