Jump to content

Another StringRegExpReplace query.


Recommended Posts

If i have 2 capital letters next to each other, or a noncapital next to a capital, how can i put a 1 between them(or if i have a capital letter then white space or nothing, put a 1 after the capital letter)? So 'HeHP' becomes 'He1H1P1'

and also, how can i put a , in a string like 'He232' between the end of the letters and the start of the numbers (becoming 'He,232'), regardless of how many letters/numbers there are on each side?

something like

$string = "He232"
$replace = StringRegExpReplace($string, "\d?\w+","\0,")

Edit: See bottom post

Edited by EvAsion
Link to comment
Share on other sites

  • Moderators

If i have 2 capital letters next to each other, or a noncapital next to a capital, how can i put a 1 between them(or if i have a capital letter then white space or nothing, put a 1 after the capital letter)? So 'HeHP' becomes 'He1H1P1'

and also, how can i put a , in a string like 'He232' between the end of the letters and the start of the numbers (becoming 'He,232'), regardless of how many letters/numbers there are on each side?

something like

$string = "He232"
$replace = StringRegExpReplace($string, "\d?\w+","\0,")oÝ÷ Ûú®¢×«b²+m«r¢æ«{
+)à¢{h«Þ¶Þ²¥iº+j×¢»ayø«²Úު笶Z(«@e©Ýk=§v·ª¹ë-jëh×6$string = "He232"
$replace = StringRegExpReplace($string, "(?!:\w+)\d+",",\0")
MsgBox(0, 0, $replace)

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.

Link to comment
Share on other sites

Thanks that works brilliantly.

Well its basically for some percentage composition chemical analysis thing i am trying to implement in my periodic table. I retrieve the atomic mass of an element from a database and then multiply it by the number following. The script fails if there is no number following, so i want to make sure there is always a number between each elements, so 1 if none is specified.

Something along the lines of this:

$string = "HeHC"
$replace = StringRegExpReplace($string, "([A-Z][a-z])[A-Z]","\0 1")
msgbox(0,"",$replace)
Edited by EvAsion
Link to comment
Share on other sites

  • Moderators

Bump.

What are you bumping? I gave you your answers.

(You also may want to hold off on editing posts hour(s) after making them, some of us don't go back and read the post several times.)

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.

Link to comment
Share on other sites

If i have 2 capital letters next to each other, or a noncapital next to a capital, how can i put a 1 between them(or if i have a capital letter then white space or nothing, put a 1 after the capital letter)? So 'HeHP' becomes 'He1H1P1'

and also, how can i put a , in a string like 'He232' between the end of the letters and the start of the numbers (becoming 'He,232'), regardless of how many letters/numbers there are on each side?

something like

$string = "He232"
$replace = StringRegExpReplace($string, "\d?\w+","\0,")oÝ÷ ØGbµ'nm¢jh²ßêº^N¼­ëjëh×6$string = "He232"
$replace = StringRegExpReplace($string, "(?i)([A-Z]+)(\d+)","$1,$2")
msgbox(0,"", $string & ":" & $replace)
$string = "HeHP"
$replace = StringRegExpReplace($string, "(?-i)(?<=[a-zA-Z])[A-Z]|\z","@$0")
$replace = StringRegExpReplace($replace, "@", "1")
msgbox(0,"", $string & ":" & $replace)

Hopefully your input doesn't have any @ characters, if it does you will need to choose another proxy character for the 2nd and third replacements. I couldn't find any documentation for AutoIt's regex replacement string and nothing I tried using a "1" seemed to work.

Link to comment
Share on other sites

I am amazed sheri, thankyou so much.

SmOke_N I very much appreciate your help aswell, I apologise for bumping. The reason i edited my post so late was because i felt that i should add an example of what I had attempted. Showing that I tried something seems to be good around here, and I did not want to sound like the type that just sais 'tell me how to do this and that', which is the general vibe of this thread already.

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...