Jump to content

[REGEX] Phrase with comma at the end


Recommended Posts

I need an help with StringRegExpReplace. I'll post a list of phrases and expected result:

My Word  --> My Word (nothing change)

My Word,  --> My Word (remove last comma if not followed by a word)

My Word,  --> My Word (remove last comma and every space after the comma if are not followed by a word)

My Word,AB  --> My Word,AB (allowed only if 2-letter char follow the comma, nothing change)

My Word, AB  --> My Word,AB (allowed since 2-letter char follow the comma but remove the space)

In synthesis, if there is a comma, need to follow to that comma 2-letter word, if there is a space after comma and follow the 2-letter rule remove it. If not follow this rule, remove everything after the comma

My Word, ABC --> My Word

My Word,ABC --> My Word

P.S. Isn't possible to have a situation with two commas in my test phrases, so at least is more "easy"

I think I was clear no? I'll hope :D 

Thanks

 

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

Try this.

Local $sTestStr = _
        "My Word" & @CRLF & _      ; --> My Word    (nothing change)
        "My Word," & @CRLF & _     ; --> My Word    (remove last comma if not followed by a word)
        "My Word, " & @CRLF & _    ; --> My Word    (remove last comma and every space after the comma if are not followed by a word)
        "My Word,AB" & @CRLF & _   ; --> My Word,AB (allowed only if 2-letter char follow the comma, nothing change)
        "My Word, AB" & @CRLF & _  ; --> My Word,AB (allowed since 2-letter char follow the comma but remove the space)
        "My Word, ABC" & @CRLF & _ ; --> My Word
        "My Word,ABC"              ; --> My Word

Local $s = StringRegExpReplace($sTestStr, "(?!,)\h+", "") ; Remove spaces following a coma,
$s = StringRegExpReplace($s, "(?im),([a-z]|.{3,})?$", "") ; Remove the coma and the following one letter, or, three or more characters to end of line.
ConsoleWrite($s & @CRLF)

 

Link to comment
Share on other sites

@Malkey

I didn't test it, I'll do it very soon. I have one line at time to process, not like your example, so maybe is possible to do everything in one pattern of the regex and not with 2 different passages?

Thanks

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

Try this:

Local $aTestStr = [ _
    "My Word", _       ; --> My Word    (nothing change)
    "My Word,", _      ; --> My Word    (remove last comma if not followed by a word)
    "My Word, ", _     ; --> My Word    (remove last comma and every space after the comma if are not followed by a word)
    "My Word,AB", _    ; --> My Word,AB (allowed only if 2-letter char follow the comma, nothing change)
    "My Word, AB", _   ; --> My Word,AB (allowed since 2-letter char follow the comma but remove the space)
    "My Word, ABC", _  ; --> My Word
    "My Word,ABC" _    ; --> My Word
]

For $s In $aTestStr
    $s = StringRegExpReplace($s, "(, *$|(?<=,) (?=[A-Z]{2}$)|, *[A-Z]{3,}$)", "")
    ConsoleWrite($s & @CRLF)
Next

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Thanks jchd for your clarity.

I experimented with your method.

; https://www.autoitscript.com/forum/topic/193797-regex-phrase-with-comma-at-the-end/?do=findComment&comment=1390114

Local $sREPattern = "(?im)(,\h*[A-Z]?$|,\h*[A-Z]{3,}$|(?<=,)\h+(?=[A-Z]{2}$))"
Local $sTestStr = _
        "My Word" & @CRLF & _       ; --> My Word    (nothing change)
        "My Word," & @CRLF & _      ; --> My Word    (remove last comma if not followed by a word)
        "My Word,  " & @CRLF & _    ; --> My Word    (remove last comma and every space after the comma if are not followed by a word)
        "My Word,AB" & @CRLF & _    ; --> My Word,AB (allowed only if 2-letter char follow the comma, nothing change)
        "My Word,  AB" & @CRLF & _  ; --> My Word,AB (allowed since 2-letter char follow the comma but remove the space)
        "My Word,  ABC" & @CRLF & _ ; --> My Word
        "My Word, A" & @CRLF & _    ; --> My Word
        "My Word,ABC"               ; --> My Word
Local $s = StringRegExpReplace($sTestStr, $sREPattern, "")
ConsoleWrite($s & @CRLF)
ConsoleWrite("---------------------" & @CRLF)

Local $aTestArray = [ _
        "My Word", _       ; --> My Word    (nothing change)
        "My Word,", _      ; --> My Word    (remove last comma if not followed by a word)
        "My Word,  ", _    ; --> My Word    (remove last comma and every space after the comma if are not followed by a word)
        "My Word,AB", _    ; --> My Word,AB (allowed only if 2-letter char follow the comma, nothing change)
        "My Word,  AB", _  ; --> My Word,AB (allowed since 2-letter char follow the comma but remove the space)
        "My Word,  ABC", _ ; --> My Word
        "My Word,  A", _   ; --> My Word
        "My Word,ABC" _    ; --> My Word
        ]
For $s In $aTestArray
    $s = StringRegExpReplace($s, $sREPattern, "")
    ConsoleWrite($s & @CRLF)
Next

 

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