Regex Anyone?

guddler

Busting vectors like it's 1982!
vacBacker
Feedback
10 (100%)
Credits
4,055CR
Anyone any good at regular expressions? Not entirely off topic, I've been fiddling about with syntax highlighting for the TL/1 language (Fluke 9100).

Most of it is easy enough and just taking time but I wanted to pick up on illegal hex entry since hex numbers have to be in the form 0xFF(etc) and the letters must be upper case.

I'm new to regular expressions so I've been trying for a day now and I can't work out for the life of me how to detect lower case letters anywhere in the 'word'. I thought something like:

Code:
b0x[^0-9A-F]+b
would be along the right lines, but the problem is that as soon as you have a valid upper case letter anywhere in the string it thinks it's valid. So, 0xffFf returns valid.

Anyone good enough to help with this please?

The other thing is that I want to restrict it to 8 characters (32bit) as well but I won't loose any sleep if I don't get that working.

Has to be regular expressions unfortunately too.
 

trm

Who loves you, and who do you love?
Feedback
2 (100%)
Credits
2,876CR
OK, as a first run how about dropping the "one or more" + metachar, and the subsequent ^ "start of" metas and replace it with a kludge of

b0x[^0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F][0-9A-F]b

And just enforce a regexp for all 8 characters?

If that doesn't work then I'll have a look at the TL/1 PDF to see how their implementation differs from POSIX which is where I learnt regexping.

Are you passing the string through the regexp or using it as an equality check?
 

guddler

Busting vectors like it's 1982!
vacBacker
Feedback
10 (100%)
Credits
4,055CR
The regexp is being run by the text editor (TextMate in this case) and it's validating as you type. I'm not too sure what you mean by checking the TL/1 pdf to see how their implementation differs as it's not their implementation
smiley2.gif


Ok, as for the regexp, 0xF is valid, as is 0xFF, right up to 0xFFFFFFFF, but any lower case digits are not. So really

Code:
b0x[^0-9A-F]{,8}b

seems just as valid as the kludge to me, yet any valid u/c character prevents a match, even if there are invalid ones too...

It may be that I'm not explaining it too clearly!!

Have you got your 9100 yet?
 

trm

Who loves you, and who do you love?
Feedback
2 (100%)
Credits
2,876CR
Not got my 9100 yet as I realised I wasn't going to have any time at all in the next year or so to give it the effort it requires, and for anything I can't use a probe or analyser for I'd still got my 9010a of which I'm nowhere near to pushing the boundaries.

Although I'm tempted to treat myself to the 9100 as an xmas pressie. Dad has his up and running and is churning out quite a bit of code which'll be up on planB shortly. Maybe not the best code ever as he's still learning (as he's not really got anybody/anything to compare to) but hopefully it'll stimulate some discussion.
 

trm

Who loves you, and who do you love?
Feedback
2 (100%)
Credits
2,876CR
I deleted my post that showed my reading comprehension has been severely affected by this suspicious smelling soldering iron.

I'm off to go try write a regexp to use with sed and will be back when I have a worker.
 

pobtastic

Moderator
Staff member
vacBacker
Feedback
2 (100%)
Credits
477CR
Ummm... You do know that when you use ^ inside of square brackets it means ! NOT !

Use something like this; ^0x[0-9A-F]{2,4}$

In this context ^ means start of, so match '0x', followed by between 2 to 4 characters 0-9 or A-F.

I think you need to Google regex cheat sheet
smiley2.gif
This helps for reference; http://rubular.com

Pobster
 

pobtastic

Moderator
Staff member
vacBacker
Feedback
2 (100%)
Credits
477CR
^0x(?:[0-9A-F]{2}){1,4}$

Ninja regular expression skillz!
smiley2.gif


Match up to eight hex chars but only if they're in blocks of two!

Pobster
 

guddler

Busting vectors like it's 1982!
vacBacker
Feedback
10 (100%)
Credits
4,055CR
I'll try that in the mornin'

The reason for having the caret inside the square brackets is because I'm doing syntax highlighting and I'm trying to highlight INVALID syntax. Hence I want to highlight black on a red border a hex number that a) contains lower case b) is greater than 8 digits. An odd number of digits is allowed on the Fluke I think but I will have to check that...

[EDIT] Doesn't work. I need to explain better. About to watch a film now though
smiley1.gif


Quickly...

Valid:

0xFF

0x1F

0xF1

0xF01

(etc.)

Invalid:

0xf

0xFf

0x1f

0xf10

(etc.)

All invalid because of the lower case letter.

I want to match INVALID strings, not correct strings.

Problem I'm finding is that the string is not matching if there is 1 (or more) VALID characters in there, even when there are still INVALID characters, and therefore should match.

(Oh dear, I can see this being very difficult to explain to anyone that hasn't used a 9100!)

guddler2010-12-04 23:06:12
 

pobtastic

Moderator
Staff member
vacBacker
Feedback
2 (100%)
Credits
477CR
Nah, I get it! Loons, that's too easy, you simply use;

[a-f]+

If it matches a-f one character or more across the entire string, then it's a match. It works because you lose the ^ = 'starts from' and $ = 'end at'. You don't have to match the whole string for it to be a hit!

Check it out: http://rubular.com/r/xgfmthBxFE

Pobster

pobtastic2010-12-05 11:46:38
 

guddler

Busting vectors like it's 1982!
vacBacker
Feedback
10 (100%)
Credits
4,055CR
Apart from it doesn't work, it's probably the first thing I tried!...

Screen_shot_2010-12-05_at_11.56.03.png


They should all be highlighted, fully. (The ok, or not ok refers to whether it's working or not)

PS: At least this shows it's not just me barking mad!

guddler2010-12-05 11:58:56
 

guddler

Busting vectors like it's 1982!
vacBacker
Feedback
10 (100%)
Credits
4,055CR
The reason that doesn't work is because we're saying immediately following '0x' match 0 or more (maybe 1 or more, can't remember what the + is) lower case a through f's. You actually need to add a question mark after the plus to make it lazy or it will match the whole comment as well.

The problem with that is that as soon as it hits an upper case F it stops matching.
 

guddler

Busting vectors like it's 1982!
vacBacker
Feedback
10 (100%)
Credits
4,055CR
Who's the ninja now
smiley1.gif

Code:
match = '$([^s]*?)([^0-9A-F ])([^s]*?)b';

Screen_shot_2010-12-06_at_00.57.08.png


Ok, so getting there...

Regex for invalid hex done, on to other issues! Have to admit, that was a bitch though
smiley36.gif


At least I have a better understanding of regular expressions now.

guddler2010-12-06 01:04:46
 

guddler

Busting vectors like it's 1982!
vacBacker
Feedback
10 (100%)
Credits
4,055CR
PS: If you know how to add the "no more than 8 digits long (plus the $)" into that mix then I'd love to add it but the way that stands at the moment, I'm not sure that it's possible.
 

pobtastic

Moderator
Staff member
vacBacker
Feedback
2 (100%)
Credits
477CR
Uhhh... I wasn't matching the 0x at all - it's a little pointless! Think simplicity... You'd just do two matches;

^0x[A-F0-9]{0,8}

[a-f]+

Still king of regex!
smiley20.gif


Pobster

pobtastic2010-12-06 10:33:38
 

guddler

Busting vectors like it's 1982!
vacBacker
Feedback
10 (100%)
Credits
4,055CR
Can't do two matches. It's not going into freeform code that I'm writing, it's going into a plugin bundle for a text editor.

I don't think you're really looking at the example images I'm posting up!

Each regex is being applied to a line of text in an editor. This means that you can't use the caret at the beginning of a regexp as the hex string may not be at the beginning of a line. In fact it's probably accurate to say that it's never going to be.

You also can't have "[a-f]+" as the chances of a hex string being at the end of a line are slim(ish), hence me putting the comments at the end of the lines in the screenshot. Anything with a + in it will do a greedy match and shoot right off into whatever follows the hex string. Hence my use of '*?' to make it lazy.
 

pobtastic

Moderator
Staff member
vacBacker
Feedback
2 (100%)
Credits
477CR
?!!! You need to use '$' to match the end of a line, so "|[a-f]+|" matches *any* lower-case throughout the entire string. It's not limited to either a '^' beginning or an '$' end! To match only the end it'd have to be "|[a-f]+$|".

KING, go with it!
smiley40.gif


I guess all this is a bit redundant now anyways, as you said - you've already solved it!

Pobster

pobtastic2010-12-06 12:03:18
 

guddler

Busting vectors like it's 1982!
vacBacker
Feedback
10 (100%)
Credits
4,055CR
I don't want to match ANY lower case throughout the ENTIRE string. If I did that then in this example...
Code:
<somestuff>$0FfF	<some more stuff>	!this is a comment

Would match "this is a comment" too. I only want to match the lower case 'f'

Anyway, after a couple days dicking about, the matching side of it works fine now
smiley1.gif
 

pobtastic

Moderator
Staff member
vacBacker
Feedback
2 (100%)
Credits
477CR
Bah, you finally ask something programming related that I can actually help with and then you go and sort it out yourself
smiley13.gif
No fair!!!

Pobster
 
Top