Jump to content

Use StringRegExpReplace to replace outside comments and quotes?


Recommended Posts

Hi

I've mentioned before that I implement LIS systems for hospitals.

I have been writing a syntax checker for one of the parts of this implementation. It helps write what could be considered "macros" that take complex tasks that would normally take a body to do and has the computer do it.

I've ran up on a part in the autoformatting portion of the checker where I want it to do some formatting outside of quotes or outside of comments, but not within them.

For example, I want it to add spaces in front of and behind general mathmatic functions (+,-,*,/, etc).

the current method I'm using is to scan the script and replace each quoted item or commented item with a variable, then adding the spaces for the math functions and then going back and replacing the variables above with the original quoted or commented item. While this works, I'm wondering if a straight regex can be written that would do it without all the swapping and replacing.

In the snippet script example below there are items which need the spaces (uamicmin-curinemin) but others that do not ("5-10" for example)

RULE

(

UAMIC.FINDTEST AND

CONTRIBCHANGED AND CONTRIBPRESENT AND

NOT UAMIC.ANYTESTCOM("CURINE REFLEXED BY RULE CURINE_S") AND

NOT UAMIC.ANYTESTCOM("CURINE REFLEXED BY RULE UACI_REFL_S") AND

(

NOT CURINE.FINDTEST OR

(CURINE.FINDTEST AND ABS(UAMICMIN-CURINEMIN) >= TIMELIMIT)

) AND

(

(

MATCH(SITE_CODE,"LR") AND MATCH(TESTREQ.PTCODE,"LRER") AND

(

MATCH(UAWBC.VALUE,"5-10","10-20","20-30","30-40","40-50","50-100","100-180","AB180","TNTC")

) AND

UAMIC.ANYTESTCOM("MICROSCOPIC REFLEXED FROM UA")

) OR

(

MATCH(SITE_CODE,"MR") AND MATCH(TESTREQ.PTCODE,"MRCOUR","MRSERI") AND

(

MATCH(UAWBC.VALUE,"5-10","10-20","20-30","30-40","40-50","50-100","100-180","AB180","TNTC")

) AND

UAMIC.ANYTESTCOM("MICROSCOPIC REFLEXED FROM UACI")

) OR

(

MATCH(SITE_CODE,"CH") AND

(

MATCH(UAWBC.VALUE,"5-10","10-20","20-30","30-40","40-50","50-100","100-180","AB180","TNTC") OR

(NOT UABAC.MISSING AND UABAC.DATACHANGED("VALUE") AND MATCH(UABAC.VALUE, "2+", "3+", "4+"))

) AND

UAMIC.ANYTESTCOM("MICROSCOPIC REFLEXED FROM UACI")

)

)

)

{

ENTORDER.ADDTEST("CURINE");

UAMIC.TESTCOMMENT("N","CURINE REFLEXED BY RULE CURINE_S");

REFTEST.TESTCOMMENT("N","TEST REFLEXED FROM ACCN: " & UAMIC.ACCESSION);

}

What I'm using now is:

;~strip // comments and quoted items

local $arr = StringRegExp($_cont, "(?://.+rn)|((?:x22[^x22]*x22)+)", 3)

;~ unique items

local $newarr = _ArrayUnique($arr)

;~ _ArrayDisplay($newarr)

;~ replace with variables

for $tX = 1 to $newarr[0]

$_cont = stringreplace($_cont,$newarr[$tX],"#newarr" & $tX & "#")

next

;~ add spaces

$_cont = StringRegExpReplace($_cont, "()|(|d|w|x22)(<|>|=|+|-|*)", "$1" & " " & "$2")

;~ put it back together

for $tX = 1 to $newarr[0]

$_cont = stringreplace($_cont,"#newarr" & $tX & "#", $newarr[$tX])

next

can all of that be done with a single regex?

tia

BT

Edited by brycetech
Link to comment
Share on other sites

Can you provide text before, text after "the regex" so that I can test it on my own.

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

I don't have a clear idea about the grammar behind, but something I'd look at is the fact that >= will now get a space between > and =, possibly changing the meaning of the test by making them now two separate tokens for the interpretor. That could be of importance in this context.

As to do all this in one shot, I'm not sure. Mainly because the use of double quote in the "formatting" regep is unclear to me.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Hi

thanks for your replies

I'll attach an example of a full rul, before formatting and after. This is an extreme case of what could occur, sometimes there are a correct spaces where they should be.

I can get the tabs to work, so feel free to ignore them in the "after" file.

Notice that only the items outside quotes or comments will be changed to have the addional spaces around the mathmatic operators.

If I could just get a guide or some pointers on how to start making this work, I think I could go from there.

and yes, the comment above about >= or <= or <> or == is valid, there should not be spaces between them if they appear together in the order indicated.

=> or =< is not a valid operator in this language nor is +- or some other that other languages permit

I'm already scanning for these before this part cause they can crash the server

While my program adds the spaces, I just think there 's gotta be a better way than all the swapping I'm doing.

any help is appreciated.

tia

BT

before.txt

after.txt

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...