Recognize syntax constructions in text [message #524631] |
Thu, 01 April 2010 16:01 |
Stella Levin Messages: 89 Registered: July 2009 |
Member |
|
|
Hi,
I need to recognize in text expressions like the following and get names A, B:
input A;
input [7:0] A;
output B;
input [3:0] B;
May I use RuleBasedScanner class for it? I tried:
RuleBasedScanner scanner = new RuleBasedScanner();
IToken tok = Token.UNDEFINED;
IRule[] rules = new IRule[4];
rules[0] = new SingleLineRule("input", ";", tok);
rules[1] = new SingleLineRule("output", ";", tok);
But it doesn't return meaningfull tokens. May you help with some code snippet.
Thanks, Stella
[Updated on: Thu, 01 April 2010 16:02] Report message to a moderator
|
|
|
|
Re: Recognize syntax constructions in text [message #525311 is a reply to message #525307] |
Tue, 06 April 2010 07:54 |
Eclipse user Messages: 49 Registered: July 2009 |
Member |
|
|
Le 06/04/2010 09:46, Eclipse user a écrit :
> Le 01/04/2010 18:01, Stella a écrit :
>> Hi,
>> I need to recognize in text expressions like the following and get names
>> A, B:
>> input A;
>> input [0:7] A;
>> output B;
>> input [0:3] B;
>> May I use RuleBasedScanner class for it? I tried:
>> RuleBasedScanner scanner = new RuleBasedScanner();
>> IToken tok = Token.UNDEFINED;
>> IRule[] rules = new IRule[4];
>> rules[0] = new SingleLineRule("input", ";", tok);
>> rules[1] = new SingleLineRule("output", ";", tok);
>> But it doesn't return meaningfull tokens. May you help with some code
>> snippet.
>
> No code snippet, but you could use regular expressions:
>
> String regexp = "(input|output) (\\[\\d+:\\d+\\]) ([a-zA-Z])";
>
> Pattern p = Pattern.compile(regexp);
> for each line {
> Matcher m = p.matcher(line);
> boolean b = m.matches();
> if (b) {
> boolean isInput = m.group(1).equals("input");
> String name = m.group(3);
> //...
> }
> }
>
> see
> http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Patt ern.html#sum
you can use this website to improve the regexp:
http://myregexp.com/
This one is better : (input|output) ((\[\d+:\d+\]) )?([a-zA-Z]) and
don't forget to double backslash '\\' in java code.
|
|
|
Powered by
FUDForum. Page generated in 0.02920 seconds