Sign in to follow this  
Followers 0
Josbe

[RESOLVED] Extracting some values with StringRegExp()

10 posts in this topic

#1 ·  Posted (edited)

Hi, I'm working in some code and I need extract some values with RegExp, my actual code is like this:

$sLine = "somecharsOrSymbols===someCharsOrSymbols"
$sRes= StringRegExp( $sLine, "(.*)===(.*)", 1)

I would like excerpt two values between a tripple equal symbol (===), in most cases the code above works, but cases like "4 equals" not.

E.g.:

SomeTextWithOrNotSymbolsBefore===SomeTextWithOrNotSymbolsAfter

Results what I need:

$sRes[0] => "SomeTextWithOrNotSymbolsBefore"

$sRes[1] => "SomeTextWithOrNotSymbolsAfter"

RULES:

NOT Allow:

sometext====sometext

sometext==sometext

Allowed:

sometext===some=text

sometext===s=ome=text

some==text===sometext

some text with spaces===some text with symbols %,.==

I would like an efficient pattern for the previous rules, because I'm not good with RegExp. :mellow:

Thxs.

Edited by Josbe

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

$s_val_1 = "Or"
$s_val_2 = "Not"
$s_line = "somecharsOrSymbols===someCharsOrSymbols"
$s_res = StringRegExpReplace($s_line, "(.*?)(\b={3}\b)(.*?)", "\1" & $s_val_1 & $s_val_2 & "\3")
ConsoleWrite($s_res & @CRLF)

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

$s_val_1 = "Or"
$s_val_2 = "Not"
$s_line = "somecharsOrSymbols===someCharsOrSymbols"
$s_res = StringRegExpReplace($s_line, "(.*?)(\b={3}\b)(.*?)", "\1" & $s_val_1 & $s_val_2 & "\3")
ConsoleWrite($s_res & @CRLF)oÝ÷ Ûú®¢×êÞ¦Vx"·l¶§Æî¶f²mâ7öl+¢x"Ëaz·,ÊÚÆÚÚrÛazö¥¹ë.²)àEèÆÛ    «¬zl­zËlºÈ§*.®­µêçÁ«ºÚ"µÍÚ[ÛYH  Ð^K]LÉÝÂÌÍÜ×Û[HH   ][ÝÐÛÛÜOOTY    ][ÝÂÌÍÜ×ÜÈHÝ[ÔYÑ^
    ÌÍÜ×Û[K    ][ÝÊÊJ   ÌLØ^ÌßIÌLØJÊI][ÝËJBÐ^QÜ^J    ÌÍÜ×ÜË    ][ÝÝÝ[É][ÝÊ

Output:

I would like this:

Output:

*I'm using the flag 1 to return an array.

Share this post


Link to post
Share on other sites

$s_line = "somecharsOrSymbols===someCharsOrSymbols"
$a_res = StringRegExp($s_line, "(.*?)\b={3}\b(.*?)\z", 1)
_ArrayDisplay($a_res)


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

My 5 cents:

#include <Array.au3>

$s_line = "somecharsOrSymbols===someCharsOrSymbols"
$a_res = StringRegExp($s_line, "(.*?)=+(.*)", 1)
_ArrayDisplay($a_res)

:mellow:

Not 1,2,4,5,6,7... but only 3

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

Not 1,2,4,5,6,7... but only 3

:mellow: My miss, sorry :(

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

You know Smoke_N that is a good example but that got me to thinking about how we are doing these. Look at the example I'm going to give you:

"Random Text somecharsOrSymbols===someCharsOrSymbols Random Text"

"Random Text somecharsOrSymbols===someCharsOrSymbols"

"somecharsOrSymbols===someCharsOrSymbols Random Text"

"somecharsOrSymbols===someCharsOrSymbols"

With these four examples can you accomplish this task all in a single command line? I also added a . after the word and before therefore adding another twelve more to the list therefore ending with sixteen total. Now knowing that I can't go off the "Random Text" because well it is random I lookup the pcrepattern specification and found the lookbehind assertions, but I found out that you can't use backwards compution on a unknown value. So back to the board I go, after thinking about this I came up with a character solution.

$aResult = StringRegExp($sValue, "(?:\B)(\w*?)\b={3}\b(\w*?)(?:\W|\z)", 1)

This script copies the first word and checks the second word if it is a a-zAZ or "_" is it is true then finds three characters of = then searches again for the word a-zA-Z or "_". Now I finally get to the part the stumps me using the \W, as not in finding a-zA-Z or "_" but any other character. I could use a "." but then what about the other end caps? Now I could put .|?|!|,|'|"|:|;|[|{|]|} covering all the possible outcomes but then wouldn't I need to do the same for the character and symbols put down every symbol I know of?

Edited by TerarinK

0x576520616C6C206469652C206C697665206C69666520617320696620796F75207765726520696E20746865206C617374207365636F6E642E

Share this post


Link to post
Share on other sites

I got it btw

#include <array.au3>

$sValue = "Hello, my name is test. text text .somecharsO$rSymbols===someCh$arsOr_Symbols. text"
$aResult = StringRegExp($sValue, "(?:\B)([a-zA-Z#-&_@]*?)\b={3}\b([a-zA-Z#-&_@]*?)(?:(?:[!""'-),.:;?]|\s)|\z)", 1)
_ArrayDisplay($aResult)oÝ÷ Ù8^­ì²z-~éܶ*'²­æ­yÙ²£   ì¢+r¢ìÛhr·µë(çîËb¢wè®)íêÞ¶§Ê§yçm¡×ªÞ¶¦¢{h¥©Ýë-¦VzØ^uç%j¶­âØ^~éܶ*')¶¬jëh×6#include <array.au3>

$sCharSymbol = "([a-zA-Z#-&_@]*?)"
$sEndStatement = "[!""'-),.:;?]"

$sValue = "Hello, my name is test. text text .somecharsO$rSymbols===someCh$arsOr_Symbols. text"
$aResult = StringRegExp($sValue, "(?:\B)" & $sCharSymbol & "\b={3}\b" & $sCharSymbol & "(?:(?:" & $sEndStatement & "|\s)|\z)", 1)
_ArrayDisplay($aResult)

0x576520616C6C206469652C206C697665206C69666520617320696620796F75207765726520696E20746865206C617374207365636F6E642E

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  
Followers 0