Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Put here requests of problems with regular expressions


  • Please log in to reply
1074 replies to this topic
TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

edit:

Just realized my needle was in reverse: ([\\``])(\w)|(``"")

Reworking the whole thing....


Posted Image

don't duplicate, iterate!


TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

i cant stand auto concat, the . operator makes code so much more readable

 

I agree that this operator is good to have considering so many other languages do.

My only concern is that older code will break.


Posted Image

don't duplicate, iterate!


adrianh
  • Members
  • 616 posts
  • Last active: Apr 07 2016 03:35 PM
  • Joined: 28 Oct 2012

Hmmmm, a custom stress test is a good idea, but I'm still having some difficulty trying to understand your rational.

 

So, you have a string in AHK that you want to convert to JSON, and JSON is basically an exchange format which is a string (Forgive me, I don't know much about JSON).

 

So, a string you will be converting would be something like "``n" which you want to convert to "\n"?

 

What I don't understand is why would you have a string like "``n" in the first place?  I could understand having "`n" but not having "``n".  Do you follow me?

 

 

Adrian


my library base
AHK_L is the bomb! With a whole lot of bug fixes, Unicode support, associative array objects, array like objects, classes and variadic functions, why wouldn't you switch?


adrianh
  • Members
  • 616 posts
  • Last active: Apr 07 2016 03:35 PM
  • Joined: 28 Oct 2012

It would help to have an example of a valid JSON string that you are thinking of, if only just what you mean.

 

 

Adrian


my library base
AHK_L is the bomb! With a whole lot of bug fixes, Unicode support, associative array objects, array like objects, classes and variadic functions, why wouldn't you switch?


TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

Adrian,

Thanks for your effort but I figured out RegEx cannot actually be used to complete this task.


Posted Image

don't duplicate, iterate!


adrianh
  • Members
  • 616 posts
  • Last active: Apr 07 2016 03:35 PM
  • Joined: 28 Oct 2012
Really? I find that surprising. With a combination of CallOuts and maybe some recursive patterns, it almost anything should be possible.


Adrian

my library base
AHK_L is the bomb! With a whole lot of bug fixes, Unicode support, associative array objects, array like objects, classes and variadic functions, why wouldn't you switch?


adrianh
  • Members
  • 616 posts
  • Last active: Apr 07 2016 03:35 PM
  • Joined: 28 Oct 2012
Hey, something I found interesting. The Anchor option (A) will anchor to the beginning of each replacement where as the assertion (^) only does to the beginning of the string or line.

i.e.
msgbox % RegExReplace("123 456 789", "A)(\d)\d\d ?", "$1") ; outputs "147"
msgbox % RegExReplace("123 456 789", "^(\d)\d\d ?", "$1") ; outputs "1456 789"
msgbox % RegExReplace("123`n456`n789", "`nm)^(\d)\d\d\R?", "$1") ; outputs "147"


Adrian

my library base
AHK_L is the bomb! With a whole lot of bug fixes, Unicode support, associative array objects, array like objects, classes and variadic functions, why wouldn't you switch?


smorgasboard
  • Members
  • 660 posts
  • Last active: Jan 14 2016 08:53 AM
  • Joined: 18 Jul 2012

thanks for this thread. just came across another problem. here it is. i have a txt file having 200 + entries like this

 

1 abc def jlk 1000 1200 300 ; serial number then some words may be two to four words and then two to three numbers separated by space

2 abc def 1000 400 ; sometimes i have merely two numbers only and not three numbers.

 

what is want is this; i get the addition of the last number with the smaller of the previous two numbers if there are two and if there are only two numbers than only the addition of last two like this

 

1 abc def jlk 1000 1200 300 | 1300

2 abc def 1000 400 | 1400

 

thanks in advance!!



smorgasboard
  • Members
  • 660 posts
  • Last active: Jan 14 2016 08:53 AM
  • Joined: 18 Jul 2012

got it solved by tidbit and geekdude

thanks to both of them

 

will surely learn the stuff soon!



sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008

got it solved by tidbit and geekdude

 

Would you mind sharing it so everyone can see what that solution is?



sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
FWIW, here's my solution:
lines=
(
1 abc def jlk 1000 1200 300
2 abc def 1000 400
)
Loop, parse, lines, `n, `r
	RegExMatch(A_LoopField,"\b(\d+)\b.*\b(\d+)\b$",n,RegExMatch(A_LoopField,"\D"))
	 , lines :=	RegExReplace(lines,A_LoopField,"$0 | " n1 + n2)
MsgBox %	lines


Alpha Bravo
  • Members
  • 1687 posts
  • Last active: Nov 07 2015 03:06 PM
  • Joined: 01 Sep 2011

there is a possibility sinkfaze missed the "smaller of the previous two numbers" part

lines=
(
1 abc def jlk 1400 1200 300
2 abc def 1000 400
)
loop, parse, lines, `n, `r 
{
	RegExMatch(A_LoopField, "^\d+\D+(.*)\s(\d+)$", m)
	loop, parse, m1, %A_Space%
		Smaller := (A_Index=1) ? A_LoopField : (A_LoopField<Smaller) ? A_LoopField : Smaller
	out .= A_LoopField " | " (Smaller + m2) "`n"
}
MsgBox % out

 

 



sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
 

there is a possibility sinkfaze missed the "smaller of the previous two numbers" part

More than a possibility I'm afraid...let's try this again.
lines=
(
1 abc def jlk 1000 1200 300
2 abc def 1000 400
)
Loop, parse, lines, `n, `r
{
	Pos :=	RegExMatch(A_LoopField,"\D"), RegExReplace(A_LoopField,"\b\d+\b","$0",count,-1,Pos)
	While	Pos :=	RegExmatch(A_LoopField,"\b\d+\b",m,Pos+StrLen(m))
		v :=	(A_Index<>count) ? ((A_Index=1) ? m : (m < v) ? m : v) : v + m
	StringReplace, lines, lines, %	A_LoopField, %	A_LoopField " | " v
}
MsgBox %	lines


Wraigrath
  • Members
  • 6 posts
  • Last active: Feb 27 2013 11:43 PM
  • Joined: 09 Dec 2012

I have a Section that I have to Check and manually update else where, So I copy the example.

 

Example: Bob Dole([email protected])

 

The AMR_ATL is the abrv form of what I need it to be so I did a

 

stringreplace, clipboard, clipboard, AMR_ATL, \TX English\ American Atlanta\, All

 

it Pastes Bob Dole(\TX English\ American Atlanta\[email protected]

 

 

 

 

How Do I get it to paste Just the \TX English\ American Atlanta\? I also have about 12 other Abreated Locales I need to put all in one so I can stream line it.

Thank you for any help.



TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006
Heres a basic Replace approach
str = Bob Dole([email protected])

AMR_ATL_ = \TX English\ American Atlanta\

clipboard := RegExReplace(str,"\(\K\w+_",AMR_ATL_)

msgbox % clipboard
A better approach adds the abbreviations to an array and then sends them to a RegEx Object
str = Bob Dole([email protected])

AbbrevArray := {"AMR_ATL_":"\TX English\ American Atlanta\"} ; << add the abbreviations to this array

RegExMatch(str,"O)"n:="\(\K\w+_",_),clipboard:=RegExReplace(str,n,AbbrevArray[_.value()])

msgbox % clipboard
The advantage of doing it this way, the match is used to retrieve the Abbreviation Array's key value.
Its much more streamline because you don't have use long list of conditions ( pseudo switch/case ).

Posted Image

don't duplicate, iterate!