Sign in to follow this  
Followers 0
d0n

StringRegExp problem

4 posts in this topic

say i have a list like

[14] John Steve IV
Steven John IX
John Victor VI

I am trying to trim everything down to the roman letters using stringregexp

The pattern here doesn't seem to work well with (John Victor VI) since they both start with V and returns a wrong group

"\[?.*?\]?\w*\s\w*?\s?(I.*|X.*|V.*)"

Share this post


Link to post
Share on other sites



IF the strings always end with the roman numbers then you can do something like:

Local $aStrings[3] = ["[14] John Steve IV", "Steven John IX", "John Victor VI"]

For $i = 0 To UBound($aStrings)-1
    ConsoleWrite(StringTrimLeft($aStrings[$i], StringInStr($aStrings[$i], " ", 0, -1)) & @CRLF)
Next

..or if you're insisting to use StringRegExpReplace then you can do:

Local $aStrings[3] = ["[14] John Steve IV", "Steven John IX", "John Victor VI"]

For $i = 0 To UBound($aStrings)-1
    ConsoleWrite(StringRegExpReplace($aStrings[$i], ".*\s(.*)", "\1") & @CRLF)
Next

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Or if you are looking for any Roman numeral:

StringRegExp ($sString, "(?:[^A-z]|\A)([I;V;X;C;D;M]+)(?:[^A-z]|\Z)", 3)
would work. It will only pick up what you want it to I think... And it's case sensitive.

Mat

Edit: But returns an array. I will modify now...

Edit2: Probably a bit more complicated than it needs to be but:

$sText = "[14] John Steve IV" & @CRLF & "Steven John IX" & @CRLF & "John Victor VI"

$sNew = StringRegExpReplace ($sText, "(?s)(.*?)(\W|\A)([IVXCDM]+)(\W|\Z)(.*?)", "\3" & @CRLF)

MsgBox (0, "Result", $sNew)
Edited by Mat

Share this post


Link to post
Share on other sites

Does this work?

Global $s_str = _
    "John Steve IV" & @CRLF & _
    "Steven John IX" & @CRLF & _
    "John Victor VI"
Global $s_pattern = "(.*?\W)([IVXLCDM]+)((?:\z|\W.*?))"
Global $s_out = StringRegExpReplace($s_str, $s_pattern, "$2")
ConsoleWrite($s_out & @CRLF)


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

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