Jump to content
Sign in to follow this  
michaelslamet

RegEX Help Needed

Recommended Posts

michaelslamet

I think this can be solve using RegEX, I think so, but I have no idea :x

Let say I have some variable like this:

$var1 = "cheap"

$var2 = "32"

$var3  = "6"

$var4 = "6"

$var5 = "8"

$maintext = "price is cheap, 16 button, 3 lines, size is 5k x 10k"

I need to check those var against the maintext.

For $var1, find and replace word after "price is ".

For $var2, find and replace word before "button"

For $var3, find and replace word before "lines"

For $var4 dan $var5, find and replace word after "size is "

So the final text will become: ""price is cheap, 32 button, 6 lines, size is 6k x 8k"

If not found the pattern, do not replace any text/words/string.

Any help and code example would be very appreciated :)

Thanks! :)

Share this post


Link to post
Share on other sites
PhoenixXL

Have a look

$maintext = "price is cheap, 16 button, 3 lines, size is 5k x 10k"

$var1 = "cheap"

$sPattern = "(?i)(price is )(\w+)"
$maintext = StringRegExpReplace($maintext, $sPattern, "\1" & $var1)


$var2 = "32"

$sPattern = "(?i)(\d+)( button)"
$maintext = StringRegExpReplace($maintext, $sPattern, $var2 & "\2")


$var3 = "6"

$sPattern = "(?i)(\d+)( lines)"
$maintext = StringRegExpReplace($maintext, $sPattern, $var3 & "\2")


$var4 = "6"
$var5 = "8"

$sPattern = "(?i)(size is )(.*)"
$maintext = StringRegExpReplace($maintext, $sPattern, "${1}" & $var4 & "k x " & $var5 & "k")

MsgBox(64, "Full replaced string", $maintext)

;or have it in one go like this
$maintext = "price is cheap, 16 button, 3 lines, size is 5k x 10k"
$sPattern = "(?i)price is (\w+)(.*?)(\d+) button(.*?)(\d+) lines(.*?)size is (.*)"
$maintext = StringRegExpReplace($maintext, $sPattern, "price is " & $var1 & "${2}" & $var2 & " button${4}" & $var3 & " lines\6size is " & $var4 & "k x " & $var5 & "k")
MsgBox(64, "Full replaced string", $maintext)

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.

Share this post


Link to post
Share on other sites
michaelslamet

PhoenixXL, thanks!!

I was thinking you're going to reply this, and I'm right :)

Let me test it and let you know.

Thanks again! :)

Share this post


Link to post
Share on other sites
michaelslamet

How about this scenario:

$var1 = "BBBBBBB"

$maintext = "(blablabla) AAAAA : my name is batman"

I need to replace the "AAAAA" with $var1 so the final text will become: "(blablabla) BBBBBBB : my name is batman"

Share this post


Link to post
Share on other sites
michaelslamet

Same like previous post, but do not replace if $var1 = ""

I understand this can be done using IF, but I wonder is it possible not using IF, but only using StringRegExpReplace

Share this post


Link to post
Share on other sites
PhoenixXL

RegEx engine replaces the capturing groups with the replace string, it doesn't have any more extensions.

you have 2 use If statements.

Edit: 

#include <Misc.au3>

Local Const $String = "hello"

$s_ReplaceString = ""
;indirectly though you have to use an If statement.
MsgBox( 64, "Return", StringRegExpReplace($String, "[aeiou]", _Iif($s_ReplaceString, $s_ReplaceString, "\0")))
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.

Share this post


Link to post
Share on other sites
PhoenixXL

You will find this interesting 

#include <String.au3>

Local $String = "hello there nice meeting you"
_Replace_Word($String, 4, "XXXX")
MsgBox(64, "Replaced", $String)


Func _Replace_Word(ByRef $String, $iIndex, $s_ReplaceWord)

    ;our SRE.....
    $String = StringRegExpReplace($String, "(^(?:\W*\w+){" & $iIndex - 1 & "}\W*)(\w+)(?s)(.*$)", "${1}" & $s_ReplaceWord & "${3}")

EndFunc   ;==>_Replace_Word

Regards :)


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.

Share this post


Link to post
Share on other sites
mikell

Or this :)

Local $String = "hello there nice meeting you"
_Replace_Word2($String, 4, "XXXX")
MsgBox(64, "Replaced", $String)

Func _Replace_Word2(ByRef $String, $iIndex, $s_ReplaceWord)
        $String = StringRegExpReplace ($String, '^(?:.*?\K(\w+)){' & $iIndex & '}' , $s_ReplaceWord) 
EndFunc

Share this post


Link to post
Share on other sites
michaelslamet

PhoenixXL, mikell, thanks, it works great!

Now another scenario:

$maintext = "(SUPER DEAL) Buy Now : blbalballa, size 9t x 2t, another blablbalbla"
$var1 = "3"
$var2 = "5"
$sPattern = "(?i)(size )(.*)"
$maintext = StringRegExpReplace($maintext, $sPattern, "${1}" & $var1 & "t x " & $var2 & "t")

msgbox(0,0,$maintext)

Above code will give result: "(SUPER DEAL) Buy Now : blbalballa, size 3t x 5t

What is the correct SRE to give this result: (SUPER DEAL) Buy Now : blbalballa, size 3t x 5t, another blablbalbla"

Tried few combination with no luck :(

I learn from this thread, I hope this read can help somebody here too.

Thanks again!

Share this post


Link to post
Share on other sites
PhoenixXL

$maintext = "(SUPER DEAL) Buy Now : blbalballa, size 9t x 2t, another blablbalbla"
$var1 = "3"
$var2 = "5"
$sPattern = "(?i)(size )([^,]*)"    ;capture everything after a "size " till any "," into second capturing group.
$maintext = StringRegExpReplace($maintext, $sPattern, "${1}" & $var1 & "t x " & $var2 & "t")

MsgBox(0, 0, $maintext)

Regards :)


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.

Share this post


Link to post
Share on other sites
michaelslamet

Thanks, PhoenixXL :)

The SRE above, if the $maintext after "2t" is not a comma, the result is mess up.

What if we want to capture anything after that, no matter it's a comma or not?

So this:

$maintext = "(SUPER DEAL) Buy Now : blbalballa, size 9t x 2t another blablbalbla"
$var1 = "3"
$var2 = "5"
$sPattern = "(?i)(size )(.*)"
$maintext = StringRegExpReplace($maintext, $sPattern, "${1}" & $var1 & "t x " & $var2 & "t")

msgbox(0,0,$maintext)

will result in: "(SUPER DEAL) Buy Now : blbalballa, size 3t x 5t another blablbalbla"

and this $maintext = "(SUPER DEAL) Buy Now : blbalballa, size 9t x 2t, another blablbalbla"

will result in: "(SUPER DEAL) Buy Now : blbalballa, size 3t x 5t, another blablbalbla"

The SRE from your post only working if the character after text "2t" is a comma

Thanks :)

Share this post


Link to post
Share on other sites
michaelslamet

Or maybe like this: after "size " found the next 2 "t" character and take all the remaining text after those 2 "t" character

Share this post


Link to post
Share on other sites
mikell

The more your requirement is specific, the more the regex must be specific

$maintext = "(SUPER DEAL) Buy Now : blbalballa, size 9t x 2t another blablbalbla"
$var1 = "3"
$var2 = "5"
$sPattern = "(?i)(size )(\d+t\h*x\h*\d+t)"   
$maintext = StringRegExpReplace($maintext, $sPattern, "${1}" & $var1 & "t x " & $var2 & "t")
MsgBox(0, 0, $maintext)

Edit

However looping through occurences as Phoenix told first can be a versatile way

$maintext = "(SUPER DEAL) Buy Now : blbalballa, size 9t x 2t, another blablbalbla"
Local $var[2] = [3, 5]
$sPattern = "(\d+t)" 
For $i = 0 to UBound($var)-1
   $maintext = StringRegExpReplace($maintext, '^(?:.*?\K' & $sPattern & '){' & $i+1 & '}', $var[$i] & "t")
Next
MsgBox(0, 0, $maintext)
Edited by mikell

Share this post


Link to post
Share on other sites
PhoenixXL

Check what you want, and tell us the pattern what it can be, so a single(if simple requirements) regex could be made satisfying the text

$maintext = "(SUPER DEAL) Buy Now : blbalballa, size 9t x 2t, another blablbalbla"
$var1 = "3"
$var2 = "5"
$sPattern = "(?i)(size )\K(\d+t x \d+t)"    ;hard-coded using the just example
$maintext = StringRegExpReplace($maintext, $sPattern, $var1 & "t x " & $var2 & "t")

MsgBox(0, 0, $maintext) 

The above is hard-coded and its not exactly a pattern that would work if you change the example rather of what you specified.

Better give us what the text could be in total.

Regards :)

.


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.

Share this post


Link to post
Share on other sites
michaelslamet

mikell and PhoenixXL, thanks a lot for this guide :)

I've learn alot about RegEx from those examples you both give, now I even can create my own simple RegEx o:)

However, now I face another pattern that I cant solve:

$var1 = "ant"

$maintext = "There is a brown fox jumping over the tree"

I want to change the word after "brown" to $var1 but...
when after the word is "jumping" OR "hiding" then replace both words.

so this text:
"There is a brown fox jumping over the tree"
will become:
"There is a brown ant over the tree"

so this text:
"There is a brown fox hiding over the tree"
will become:
"There is a brown ant over the tree"

but this text:
"There is a brown fox playing over the tree"
will become:
"There is a brown ant playing over the tree"

I need your help on this :)
 

Thanks again!

Share this post


Link to post
Share on other sites
mikell

@Phoenix

Lazy me, I didn't dig enough in the K features :D

@michaelslamet

For your fox you just need to introduce an alternation in the capturing group

$var1 = "ant"

$maintext = "There is a brown fox jumping over the tree" &@crlf& _ 
        "There is a brown fox hiding over the tree" &@crlf& _ 
        "There is a brown fox playing over the tree"

$maintext =  StringRegExpReplace($maintext, 'brown\h*\K(\w+\h*(jumping|hiding))', $var1)
Msgbox(0, 0, $maintext)
Edited by mikell

Share this post


Link to post
Share on other sites
michaelslamet

Mikell, thanks a lot :)

I would like to test it with maintext only "There is a brown fox jumping over the tree" (not 3 lines like your example),

what will be the correct SRE for that?

What is K for? K is not documented in the help file. Googling bring me this, but i could not understand: "This allows you to use the K escape in your regexes, which fools the regex engine into thinking it has only just started matching your regex. This means you can turn the inefficient replace-with-itself construct"

Share this post


Link to post
Share on other sites
michaelslamet

Aswering to my last post, this one is working as expected:

$var1 = "ant"
$maintext = "There is a brown fox playing over the tree"
$sPattern = "brown\h*(\w+\h*(jumping|hiding|))(\s.*)"
$maintext =  StringRegExpReplace($maintext, $sPattern, $var1 & "\3")

Msgbox(0, 0, $maintext)

May be we can simplified the SRE?

Edited by michaelslamet

Share this post


Link to post
Share on other sites
mikell

I used a 3 lines examples to test your 3 requirements together, but it works on a single line too

$maintext =  StringRegExpReplace($maintext, 'brown\h*\K(\w+\h*(?:hiding|jumping))', $var1)

This exp is better because it uses one backreference only

*very* simply speaking, the K means "start here and forget what you have seen before"

http://www.asiteaboutnothing.net/regex/regex-php.html#k

Have a look at this nice site :)

Edited by mikell

Share this post


Link to post
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
Sign in to follow this  

×