I am currenty using this regex search pattern.
reg.searchPattern = “[^.0-9]” But I want to add a pattern that will leave the numbers and Only the last period in the string. Does anyone have a search pattern for that?
Thanks
this is not very clear, can you give some examples of what should be extracted or not ?
In a texfield, the user inputs abc$123.45g.67
I want a regex to return 12345.67 everything except the numbers and the last dot is removed.
Thanks
(I have no idea if this works or not but this is what chat GPT says)
Dim pattern As String = "(\d+)(\.\d+)?"
Dim input As String = "abc$123.45g.67"
Dim rx As New RegEx
Dim match As RegExMatch
Dim result As String
rx.SearchPattern = pattern
match = rx.Search(input)
While match IsNot Nil
result = result + match.SubExpressionString(1) // Capture digits
If match.SubExpressionString(2) <> "" Then
result = result + match.SubExpressionString(2) // Capture dot and digits if present
End If
match = match.NextMatch
Wend
// result now contains "12345.67"
Is that the only possible input pattern? Or might they add more random characters, such as €7/we12,4;5.31
Gemini gives this
\d+(?:\.\d*)?
as a pattern for this regex.
or also
\d+(?:\.\d*)?(?:[^\d.]|\b)*\.?$
if you want more than two groups of numbers to be possible in the source text
Yes, it could be any combination of letters, numbers, and characters, locale EN-US. Thanks
I will give it a try. Thanks
I’d be inclined to just brute force it. Count the number of periods with CountFields. Then character by character, copy the numbers and the nth period to another string.
I agree with Tim: you could probably craft a RegEx to do this - although I think it would require more than one, right, @Kem_Tekinay ? But I think the easiest, most understandable way is to simply loop through the string and analyze each character:
dim input as string
input="234.34oij5.345lkj42.3432jkl.67kj"
dim inputCharacters() as string
inputCharacters=input.Split("")
dim characterCount as integer
dim foundFinalPeriod as boolean
dim currentCharacter as string
dim digits as string
digits="0123456789"
characterCount=UBound(inputCharacters)
for i as integer=characterCount downto 0
currentCharacter=inputCharacters(i)
if currentCharacter="." and not(foundFinalPeriod) then
'We found the final period, set the flag
foundFinalPeriod=true
ElseIf digits.IndexOf(currentCharacter)>-1 then
'Allow all digits
else
'Erase undesired character
inputCharacters(i)=""
end
next
dim output as string
output=string.FromArray(inputCharacters, "")
-
[^0-9.\n] search for this and replace with nothing to clear out all but the digits and dot
-
(.*)\.(.*\..*) replace with \1\2
Repeat step number 2 until all but the last dot are gone.
This works for that input:
Var input As String = "abc$123.45g.67"
Var rx As New RegEx
rx.SearchPattern="\.(?![^.]+$)|[^0-9.]"
rx.ReplacementPattern=""
rx.Options.ReplaceAllMatches=True
Var result As String = rx.Replace(input) //12345.67
This is how I’d do it.
Technically, that pattern does not meet his requirements because if the text ends with a period, that period is omitted.
I’m sure OP can check if the result has no period and do a + "."
to the result (if needed). Or use another pattern.
Edit: Maybe change +$ with *$