Jump to content

Regular Expressions :(


ChrisN
 Share

Recommended Posts

:idea:Someone should figure out an easier way to match things than using regular expressions. :unsure:

I need some help matching things -- I can't seem to get my regular expressions to work. So here is what I am trying to do:

(***************)

N100 (FINISH PASS)

(150MM SAW BLADE)

M6 T0 S12000 M3

D11

( - - - - - - - - - - - - -)

(STARTMOP)

some

text

goes

here

123

456

(ENDMOP)

(***************)

N100 (Roughing PASS)

(12MM Rougher)

M6 T0 S12000 M3

D11

( - - - - - - - - - - - - -)

(STARTMOP)

some

more

text

goes

here

123

456

789

asdf

(ENDMOP)

Basically, I want to have just the hilighted parts extracted. I am trying to match them using "N100 (" and "(STARTMOP)" and "(ENDMOP)" to define what to extract, but I am having trouble getting a regular expression that matches anything - I just get errors :( Can anyone help?

(BTW, I am using GEOSoft's PCRE Toolkit to test my regular expressions)

Link to comment
Share on other sites

It would help if you posted what you tried. For example, are you loading the entire file into one string? Did you set the regex option not to individually test each line? Plus regular expressions can check for things that are ... regular, what you want to collection looks somewhat random to me.

Link to comment
Share on other sites

gotta be better ways, but this works for the given example

$string = "(***************)" & @CRLF & _
"N100 (FINISH PASS)" & @CRLF & _
"(150MM SAW BLADE)" & @CRLF & _
"M6 T0 S12000 M3" & @CRLF & _
"D11" & @CRLF & _
"( - - - - - - - - - - - - -)" & @CRLF & _
"(STARTMOP)" & @CRLF & _
"some" & @CRLF & _
"text" & @CRLF & _
"goes" & @CRLF & _
"here" & @CRLF & _
"123" & @CRLF & _
"456" & @CRLF & _
"(ENDMOP)" & @CRLF & _
"(***************)" & @CRLF & _
"N100 (Roughing PASS)" & @CRLF & _
"(12MM Rougher)" & @CRLF & _
"M6 T0 S12000 M3" & @CRLF & _
"D11" & @CRLF & _
"( - - - - - - - - - - - - -)" & @CRLF & _
"(STARTMOP)" & @CRLF & _
"some" & @CRLF & _
"more" & @CRLF & _
"text" & @CRLF & _
"goes" & @CRLF & _
"here" & @CRLF & _
"123" & @CRLF & _
"456" & @CRLF & _
"789" & @CRLF & _
"asdf" & @CRLF & _
"(ENDMOP)" & @CRLF

$array = StringRegExp($string,"(?U)\((\d.*)\)[\r\n\W\w\s]+(\(STARTMOP\)[\r\n\W\w\s]+\(ENDMOP\))",3)
For $i = 0 To UBound ($array)-1
ConsoleWrite($array[$i] & @CRLF)
Next

or, group them using 4:

$array = StringRegExp($string,"(?U)\((\d.*)\)[\r\n\W\w\s]+(\(STARTMOP\)[\r\n\W\w\s]+\(ENDMOP\))",4)

For $i = 0 To UBound ($array)-1
$aTemp = $array[$i]
For $j = 1 To UBound ($aTemp) - 1
  ConsoleWrite("MatchGroup=[" & $i+1 & "], subGroup=[" & $j & "]: " & $aTemp[$j] & @CRLF)
Next
Next
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

@jdelaney: That works for my example - I'll have to test it at work tomorrow & see if it works on more things. Thanks!

Edit: It doesn't work if the tool name (150MM SAW BLADE) doesn't start with a number. I modified it and it works now

$array = StringRegExp($string,"(?U)N100 (.*)s+((.*))[rnWws]+((STARTMOP)[rnWws]+(ENDMOP))",3)

Edited by ChrisN
Link to comment
Share on other sites

This looks more promising in this case

Have a look

#include <Array.au3>

;Dividing the keypoint will keep the pattern clear though it may require more regexes.

;Working - If the start is either (STARTMOP) match till (ENDMOP)
; orelse match the characters excluding the brackets
; if brackets are not there the match fails

$string = "(***************)" & @CRLF & _
"N100 (FINISH PASS)" & @CRLF & _
"(150MM SAW BLADE)" & @CRLF & _
"M6 T0 S12000 M3" & @CRLF & _
"D11" & @CRLF & _
"( - - - - - - - - - - - - -)" & @CRLF & _
"(STARTMOP)" & @CRLF & _
"some" & @CRLF & _
"text" & @CRLF & _
"goes" & @CRLF & _
"here" & @CRLF & _
"123" & @CRLF & _
"456" & @CRLF & _
"(ENDMOP)" & @CRLF & _
"(***************)" & @CRLF & _
"N100 (Roughing PASS)" & @CRLF & _
"(12MM Rougher)" & @CRLF & _
"M6 T0 S12000 M3" & @CRLF & _
"D11" & @CRLF & _
"( - - - - - - - - - - - - -)" & @CRLF & _
"(STARTMOP)" & @CRLF & _
"some" & @CRLF & _
"more" & @CRLF & _
"text" & @CRLF & _
"goes" & @CRLF & _
"here" & @CRLF & _
"123" & @CRLF & _
"456" & @CRLF & _
"789" & @CRLF & _
"asdf" & @CRLF & _
"(ENDMOP)" & @CRLF

Local $Condition = "(?sm)^(?:\(STARTMOP\).*?\(ENDMOP\)" & _ ;Either (STARTMOB) ... (ENDMOB)
"|" & "\(([\w ]+)\))" ;or (SOME OTHER TEXT)

ConsoleWrite( "Pattern: " & $Condition & @CRLF & "+--------------------------------------------" & @CRLF )

$array = StringRegExp($string,  $Condition , 3) ;Global Match

For $i = 0 To UBound($array) - 1
ConsoleWrite( $array[$i] & @CR )
Next
Output:
150MM SAW BLADE
(STARTMOP)
some
text
goes
here
123
456
(ENDMOP)
12MM Rougher
(STARTMOP)
some
more
text
goes
here
123
456
789
asdf
(ENDMOP)
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

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

×
×
  • Create New...