
- OT: Regular Expressions
- 06-05-2009
![]() Re: OT: Regular Expressions
| DJ Delorie | 06-05-2009 |
![]() ![]() Re: OT: Regular Expressions
| Jim Thompson | 06-05-2009 |
![]() ![]() ![]() Re: OT: Regular Expressions
| DJ Delorie | 06-05-2009 |
![]() ![]() ![]() ![]() Re: OT: Regular Expressions
| Jim Thompson | 06-05-2009 |
![]() ![]() ![]() ![]() ![]() Re: OT: Regular Expressions
| Jasen Betts | 06-06-2009 |
![]() ![]() ![]() ![]() ![]() Re: OT: Regular Expressions
| Bob Larter | 06-29-2009 |
![]() Re: OT: Regular Expressions
| Jim Thompson | 06-05-2009 |
![]() ![]() Re: OT: Regular Expressions
| Paul Hovnanian ... | 06-05-2009 |
![]() ![]() ![]() Re: OT: Regular Expressions
| Jim Thompson | 06-06-2009 |
![]() ![]() Re: OT: Regular Expressions
| Jim Thompson | 06-06-2009 |
![]() ![]() Re: OT: Regular Expressions
| Jim Thompson | 06-06-2009 |
![]() Re: OT: Regular Expressions
| Jasen Betts | 06-06-2009 |
![]() ![]() Re: OT: Regular Expressions
| Jim Thompson | 06-06-2009 |
Please Register and login to reply and use other advanced options
On Fri, 05 Jun 2009 18:05:02 -0800, "Paul Hovnanian P.E."

...Jim Thompson
--
| James E.Thompson, P.E. | mens |
| Analog Innovations, Inc. | et |
| Analog/Mixed-Signal ASIC's and Discrete Systems | manus |
| Phoenix, Arizona 85048 Skype: Contacts Only | |
| Voice:(480)460-2350 Fax: Available upon request | Brass Rat |
| E-mail Icon at http://www.analog-innovations.com | 1962 |
I love to cook with wine Sometimes I even put it in the food
...Jim Thompson
--
| James E.Thompson, P.E. | mens |
| Analog Innovations, Inc. | et |
| Analog/Mixed-Signal ASIC's and Discrete Systems | manus |
| Phoenix, Arizona 85048 Skype: Contacts Only | |
| Voice:(480)460-2350 Fax: Available upon request | Brass Rat |
| E-mail Icon at http://www.analog-innovations.com | 1962 |
I love to cook with wine Sometimes I even put it in the food
There are two very good introductory tutorials for regular expressions. The
first is by Tom Christiansen, one of the Perl gurus. His description was a
usenet post some years ago, and it's widely available around the web. Like,
for example, here:
http://www.perl.com/doc/FMTEYEWTK/regexps.html
The other is by Aaron Kuchling, a Python guru. It's also widely available -
a pdf version is here:
http://www.engr.uvic.ca/~seng265/Documentation/Python/howto-regex.pdf
Tom and Aaron both recommend using verbose mode; even though it's far more
readable and understandable, your editor will probably make it difficult. It
really is far more readable, though. Just imagine trying to troubleshoot
this relatively simple regexp if there were no spaces, no formatting, and no
comments (it parses a SPICE instantiation):
spice_instance = r"""\s*
# Instance name (one occurrence). The instance may be either a
primitive or a
# subcircuit. Subcircuit calls begin with 'x', primitive calls begin
with anything else.
(?:
(?P<primitive>
[a-wyzA-WYZ_] # Primitive names begin with
anything except 'x'
$INST_ID
)
| # OR...
(?P<subckt>
[xX] # Subcircuit calls begin
with 'x':
$INST_ID
)
)
\s+ # Whitespace
# Net list (at least one net is required)
(?P<nets>
(?:
$NODE_NAME
\s+ # followed by white space
)+
)
# Primitive or subcircuit name (one occurrence)
(?P<name>
$COMP_NAME
)
(?:\s+|$) # followed by white space or
EOL,
(?!\s*=) # but not '='
# Parameter = Value pairs or $keywords (zero or more occurrences)
(?P<params>
(?:
$PARAM_NAME # Parameter name
\s*=\s* # =
$VALUE # Value
(?:\s+|$|$) # White space or $ or EOL
)* # zero or more p-v pairs or
keywords
)
"""
-- Mike --
wrote:

My situation is more straight forward, essentially all find & replace
operations (<rns> = random number of spaces including none):
ParamName<rns>=<rns>'MathEquation'
with...
ParamName<rns>=<rns>
or renaming:
ParamName1<rns>=<rns>'MathEquation'
with...
ParamName2<rns>=<rns>
or tossing (not used by PSpice):
ParamName<rns>=<rns>'MathEquation'
to...
;ParamName<rns>=<rns>'MathEquation' \n+
Some other more subtle things...
I need to learn a scripting language, so I can process a library in
one swell foop, rather than eating days of boredom ;-)
...Jim Thompson
--
| James E.Thompson, P.E. | mens |
| Analog Innovations, Inc. | et |
| Analog/Mixed-Signal ASIC's and Discrete Systems | manus |
| Phoenix, Arizona 85048 Skype: Contacts Only | |
| Voice:(480)460-2350 Fax: Available upon request | Brass Rat |
| E-mail Icon at http://www.analog-innovations.com | 1962 |
I love to cook with wine Sometimes I even put it in the food
My situation is more straight forward, essentially all find & replace
operations (<rns> = random number of spaces including none):
ParamName<rns>=<rns>'MathEquation'
with...
ParamName<rns>=<rns>
or renaming:
ParamName1<rns>=<rns>'MathEquation'
with...
ParamName2<rns>=<rns>
or tossing (not used by PSpice):
ParamName<rns>=<rns>'MathEquation'
to...
;ParamName<rns>=<rns>'MathEquation' \n+
Some other more subtle things...
I need to learn a scripting language, so I can process a library in
one swell foop, rather than eating days of boredom ;-)
...Jim Thompson
--
| James E.Thompson, P.E. | mens |
| Analog Innovations, Inc. | et |
| Analog/Mixed-Signal ASIC's and Discrete Systems | manus |
| Phoenix, Arizona 85048 Skype: Contacts Only | |
| Voice:(480)460-2350 Fax: Available upon request | Brass Rat |
| E-mail Icon at http://www.analog-innovations.com | 1962 |
I love to cook with wine Sometimes I even put it in the food
etc, etc.
Understood - that's what a scripting language will do for you, more quickly
and efficiently than an editor can. In addition, instead of commenting out
some of those equation lines hat PSPICE can't understand, you could either
calculate the value and substitute that for the equation, or translate the
equation into something PSPICE can understand.
For the simple case you show, a regexp matching valid HSPICE parameter names
is [a-zA-Z_][a-zA-Z0-9%$#_]*, and assuming your equations are all pretty
simple and you don't need to validate them (so your existing regexp matching
the equation is good enough), the matching regexp is something like this:
[a-zA-Z_][a-zA-Z0-9%$#_]*\s*=\s*'[a-zA-Z0-9\+\-\.\*\/\(\)\_]+'
You need the parenthesis to group the parameter name and the equation:
([a-zA-Z_][a-zA-Z0-9%$#_]*)\s*=\s*'([a-zA-Z0-9\+\-\.\*\/\(\)\_]+)'
To keep the same parameter name and equation, replacing the quotes with
braces, replace that with:
= {}
-- Mike --
wrote:

it looks like sed could handle these translations easily.
<rns> is ' *' in regex.

s/\(ParamName *= *\)'\([^']*\)'/{}/;

s/ParamName1\( *= *\)'\([^']*\)'/ParamName2{}/;

s/ParamName\( *= *\)'\([^']*\)'/;ParamName{}/;
so the sed script would look like
s/\(ParamName *= *\)'\([^']*\)'/{}/;
s/ParamName1\( *= *\)'\([^']*\)'/ParamName2{}/;
s/ParamName\( *= *\)'\([^']*\)'/;ParamName{}/;
with each line repeated as often as needed.
except probably I'd put the rename and comment-out operations first
and do the translates with a catch-all,
it looks like sed could handle these translations easily.
<rns> is ' *' in regex.
s/\(ParamName *= *\)'\([^']*\)'/{}/;
s/ParamName1\( *= *\)'\([^']*\)'/ParamName2{}/;
s/ParamName\( *= *\)'\([^']*\)'/;ParamName{}/;
so the sed script would look like
s/\(ParamName *= *\)'\([^']*\)'/{}/;
s/ParamName1\( *= *\)'\([^']*\)'/ParamName2{}/;
s/ParamName\( *= *\)'\([^']*\)'/;ParamName{}/;
with each line repeated as often as needed.
except probably I'd put the rename and comment-out operations first
and do the translates with a catch-all,
- Electronic symbols for web page
- December 9, 2006, 1:21 pm
- Info on packing regular tree-like structures into rectangles?
- December 1, 2005, 11:40 pm








>>
>> On Fri, 05 Jun 2009 12:45:37 -0700, Jim Thompson
>>
>> >I'm an amateur at regular expressions :-(
>> >
>> >I know I can search for...
>> >
>> >'[a-zA-Z0-9\+\-\.\*\/\(\)\_]+'
>> >
>> >and my editor (UltraEdit) will highlight...
>> >
>> >'some_text_characters_and_math_operators'
>> >
>> >I want to do a replace operation such that I get...
>> >
>> >
>> >
>> >(In other words just change the opening and closing characters)
>> >
>> >How do I do that?
>> >
>> >(UltraEdit supports Perl, Unix and their own flavor of regular
>> >expression language.)
>> >
>> >Thanks!
>> >
>> > ...Jim Thompson
>>
>> Ben at IDM Computer Solutions (UltraEdit) came thru with "tagged"
>> regular expressions... enclose parts to be retained in (...), as in...
>>
>> '([a-zA-Z0-9\+\-\.\*\/\(\)\_]+)'
>>
>> Replace with {}
>>
>> That will save me so-o-o-o-o much grunt work processing HSpice
>> libraries into PSpice syntax.
>>
>> Next, I guess I need to learn scripting ;-)
>If you are going to be parsing complex structures, you might want to
>look at some more powerful tools. Regular expresions are OK for
>identifying simple tokens and patterns. But if you try to write RE
>search and replace scripts to rewrite anything complex, you'll get into
>trouble quickly.
>I've toyed with a Perl module called Parse::RecDescent when I needed to
>recognize complex statements and extract parameters and variables. The
>grammar specification starts off at a low level with regular expressions
>that uniquely identify operators, key words, variables, etc. Higher
>level expressions are then constructed for each expected combination of
>the above parts. Eventually, the top level grammar rule represents a
>parsable file. With each grammar rule, there can be an 'action', which
>saves or prints the tokens recognized up to that point in any fashion
>you desire.
>Get Perl and then got to http://www.cpan.org/ to pick up the above
>module.
>