Hi guys, I am making progress with regex, but I still need help.
I am trying to make a special regex for my file parsor.
Basically, I would like to check if a line contain a dependency in this form, this is an example:
@ProgrammingTools-Main:Gcc>=14.2.0(Pass1,Lto)
Where the line can start with space before, the comparator can be >=,<=,<,>,= and it can have nothing after the version OR a parenthesis that contain a list of option separated by “,”.
It is still matching even if you don’t open parenthesis :x
str = <<-HEREDOC
@ProgrammingTools-Main:Gcc>=14.2.0Pass1,Pass2,Pass3,Lto)
HEREDOC
pattern = /(@[\w\-]+):([\w\-]+)(>=|<=|=|>|<)([\d.\-]+)(?:\(?([\w,\-]*)\)?)?/
str.scan(pattern) do |match|
pp match
end
Yeah, I thought that’s what you wanted I guess two things would come handy here: more clear communication regarding the desired outcome and some energy put by you into learning yourself regular expressions xD They ain’t that hard
These regexes all think that 14.2.0Pass1 is itself a valid version, it is up to you to constrain the version format further if you don’t want this to happen
I like creating complex Regex, but I also get used to use AI on a daily basis. Didn’t you try to ask AI for that task? By just copying your first post to Claude AI, i get this one, which seems to fulfill all your requirements: