For a long time I can not parse a string, such as (h or v)? ((double double double double) or ((double, double) (double, double)))

eg:

  • h 1 1 100.5 100 (as a result, S = 'h', x1 = 1, y1 = 1, x2 = 100.5, y2 = 100)
  • v 1,1 100,100.5
  • 1.1 100.5,100 (as a result, S = defaultS, point1 = (1,1), point2 = (100.5,100))
  • 1 1 100 100.5
  • 3
    And what is necessary to parse? If you just break the line by spaces - will it suit you? Those. What result do you expect to get? - BOPOH
  • one
    I suppose that something like "((h|v) )?(\d+(\.\d+) \d+(\.\d+) \d+(\.\d+) \d+(\.\d+))|(\d+(\.\d+)\,\d+(\.\d+) \d+(\.\d+)\,\d+(\.\d+))" but need to be tested - AgentFire
  • one
    Split the line by spaces, if the length of the resulting array is 5, then S = arr [0], if 4, then the defect, and the other values ​​are also filled based on the length of the resulting array. - ReinRaus
  • one
    lf; 't so regex101.com/r/xW7iY9/3 - splash58
  • one
    @ splash58 is better to bind the end of a regular expression to $ , then the last 4 numbers are guaranteed to fall into groups 2-5, and whether optional group 1 will depend only on the text. - ReinRaus

5 answers 5

The joint efforts on the basis of the AgentFire commentary and the ReinRaus board turned out to be such a regular

 ^(?:(h|v) )?(\d+(?:\.\d+)?)(?:\s+|\s*,\s*)(\d+(?:\.\d+)?)\s+(\d+(?:\.\d+)?)(?:\s+|\s*,\s*)(\d+(?:\.\d+)?)\s*$ 

(?:(h|v) )? - captures the optional feature h / v

(\d+(?:\.\d+)?) - captures an integer or floating point number

(?:\s+|\s*,\s*) - number separator - comma or spaces

Demo and explanation in more detail

  • Thank you so much. It would be very nice if the first group returned an empty string, if there is nothing there. This would reduce further code from 5 lines to 1st :). But this is probably impossible - iRumba
  • At least, it is not obvious :( - splash58
  • a, all the rules, in c # the Matches class returns all groups, but for the first group, Success = False! ) - iRumba
  • @iRumba well and great! - splash58

Good day. Here is the solution:

 (h|v)?[ ,]?([\d.]+) 

View in the interactive editor:

https://regex101.com/r/rO0zT3/3

If the syntax is pcre, you can name the brackets

  • Almost what I wanted, but even this parsit v 1 v 1 v 100.5 v 100 . But first, I still check the line for validity. - iRumba
  • Well, there was no validation check in the question, only parsing) - Mi Ke Bu
  • There is such a thing: the answers from only the links are not considered good answers, because Links tend to break with time. Therefore, I copied your regulars right in response. In future answers, please post the code immediately in the answer (and the link can be left as a useful addition). By the way, the service is excellent, did not know about this. - Nick Volynkin
  • @MiKeBu, so the parsit is wrong :) - iRumba
  • @Nick Volynkin, about links - thanks, I will take into account - Mi Ke Bu

so everything is obvious

 ([h|v] )?\d{1,4}([:punct:]\d{1,4})? \d{1,4}(\.\d{1,4})? \d{1,4}(\.\d{1,4})? \d{1,4}(\.\d{1,4}) 
  • See the response from s_klepcha. There I wrote in the comments why such a solution does not fit. Compare with the answer marked as a solution. Suddenly it will be useful. Thank. - iRumba

try

 ([h|v] )?\d{1,4}([:punct:]\d{1,4})? \d{1,4}(\.\d{1,4})? \d{1,4}(\.\d{1,4})? \d{1,4}(\.\d{1,4})? 
  • Unfortunately did not fit. :( - iRumba
  • only it would not be bad to put spaces instead of commas and then apply \ - s_klepcha to the received string and regular schedule
  • Yes, not a comma case. This is only valid for validation, not parsing. - iRumba

That's what came

 (^([h,v]) )?((\d*(\.\d+)?)(\s|\,|$))((\d*(\.\d+)?)(\s|\,|$))((\d*(\.\d+)?)(\s|\,|$))((\d*(\.\d+)?)(\s|\,|$)) 

The problem is that there are too many subgroups. I expected it to be

  • S = group [0] OR default
  • x1 = group [1]
  • y1 = group [2]
  • x2 = group [3]
  • y2 = group [4]

But as a result, for the string v 1,1 100.9,100 got it

    1. [0-2] v
    1. [0-1] v
    1. [2-4] 1,
    1. [2-3] 1
    1. [3-4]
    1. [4-6] 1
    1. [4-5] 1
    1. [5-6]
    1. [6-12] 100.9,
    1. [6-11] 100.9
    1. [9-11] .9
    1. [11-12] ,
    1. [12-15] 100
    1. [12-15] 100
    1. [15-15] `` *

So the question remains open.

  • look at my last comment - splash58