I need regex for recognizing names which can be [a-zA-Z_]
then .
(dot) then again [a-zA-Z_]
.
I ([a-zA-Z_]+) \.([a-zA-Z_]*)
but it doesn't work. Help ?
Can anybody give me hoe to do that in JFlex ?
Asked
Active
Viewed 140 times
-1

3lectrologos
- 9,469
- 4
- 39
- 46

JaneNY
- 65
- 1
- 4
-
Are you referring to a literal dot or the dot metacharacter (matching anything but a new line)? – 3lectrologos Jan 05 '11 at 10:37
-
My last edit was **only** code formatting, the backslash was present but invisible. (even though the edit *looks* as if I have added that backslash - that wasn't me, honestly!!) – Andreas Dolk Jan 05 '11 at 10:42
-
5What do you mean by "doesn't work"? does it produce a regex compilation error? Does it fail matching something it should, or matches something it shouldn't? – Eyal Schneider Jan 05 '11 at 10:43
-
1@JaneNY - please show an (original) example name/input where this pattern doesn't match. – Andreas Dolk Jan 05 '11 at 10:58
-
@JaneNY, from your previous posts, I make up you're trying to parse, and then evaluate boolean expressions. Instead of creating a parser yourself, have you considered using an existing (and proven!) one? Or perhaps Java's built-in script-engine can evaluate them on the fly for you? – Bart Kiers Jan 05 '11 at 15:13
-
And as a personal side note, if you're going to write it yourself, I highly recommend using ANTLR instead of JFlex. If you're completely new to parser generators, I find ANTLR to be much more intuitive than JFlex. If you have previous experience with GNU-Flex, then JFlex is a good first choice, of course. – Bart Kiers Jan 05 '11 at 15:16
2 Answers
2
You need to escape the dot: "\." - otherwise, the regex parser treats it as the reserved "any char" symbol.
-- EDIT -- Now that we know that the dot IS escaped and therefore not the real problem: Are you sure the space before the dot is intentional?

Eyal Schneider
- 22,166
- 5
- 47
- 78
-
(it was escaped - the backslash shows up as soon as you appy code formatting) – Andreas Dolk Jan 05 '11 at 10:39
-
@Andreas_D: mmm... you are right. I suppose that the OP should provide more info. – Eyal Schneider Jan 05 '11 at 10:42
-
If this pattern is copy-pasted from the original code, then, yes, I bet, the space before `\.` is the actual show stopper – Andreas Dolk Jan 05 '11 at 10:59
-
2@Andreas_D, no, the white space literals are ignored in JFlex rules. If you want to match a literal space in JFlex, either create a character class containing a space `[ ]`, or quote it: `" "` – Bart Kiers Jan 05 '11 at 11:10
1
changing regexp by escaping dot and removing space.
([a-zA-Z_]+)\.([a-zA-Z_]*)
additional suggestion to drop () and use temporary identifiers
edit: increasing reputation by commenting regexp
-
[Can regex answers include more detail in the explanation?](http://meta.stackexchange.com/questions/73553/can-regex-answers-include-more-detail-in-explanation) – Jan 05 '11 at 10:38
-
3Even if it works: a downvote from me for posting (1) just a simple regexp without explanation *and* (2) not formatting that single line as code. But I can change my vote ;) – Andreas Dolk Jan 05 '11 at 10:43
-