perl - How can I make $1 return alternatives without a substitution regex? -


I recently incorporated the project into the logic of code and database elements. Business logic, such as expat, regular expressions, and function names, are recorded in the database, while reading general code such as files, making xml from xpath etc. are in code base.

Most (if not all) the methods that use regular expressions are structured like this:

  if ($ entry = ~ / $ regex /) {$ req_value = $ 1; }  

This means that only $ 1 is available and you must always have your RegEx written in your $ 1 to give you the desired results.

Problem:

The result of the following string is either

  '2.6.9-78.1.6.ELsmp (SMP)' or '2.6. 9-78.1.6.ELsmp should be ' 

based on SMP's existence. $ 1 $ entry is not enough for [0].

  $ entry [0] = qq | Linux version 2.6.9-78.1.6.ELsmp (brewbuilder@hs20-bc2-2.build .redhat.com) (GCC version 3.4.6 20060404 (Red Hat 3.4.6-10)) # 1 SMP Wednesday 24 September 05 : 41: 12 ADT 2008. | $ Entry [1] = qq | Linux version 2.6.9-78.0.5.ELsmp (brewbuilder@hs20-bc2-2.build.redhat.com) (GCC version 3.4.6 20060404 (Red Hat 3.4.6-10)) # 1 Wed Sep 24 05: 41:12 EDT 2008. |  

So my solution:

  my $ mutable = ''; My $ regex = qr / ((\ d. *?) \ S + (?:. *)? (SMP) ((? {$ Mutable = "$ 2 ($ 3)"})) | (\ *?)) \ S + /; If ($ entry [$ i] = ~ / $ regex /) {$ req_value = $ 1; $ Req_value = $ mutable if ($ mutable ne ''); $ Mutable = ''; Unfortunately, the existence of a 'variable' in the database makes this solution unacceptable.  

My questions are:

  1. or

  2. How do I replace using regex

    Thanks.

    -text "itemprop =" text ">

    Are trapped until you can not talk to those people who control the code by you that you are using to normalize in any way. The good news is that you only need a little more, maybe

      if (my @ fields = $ _ = ~ / $ pat /) {$ req_value = join "" => Defined Grep ($ _), @ fields; }  

    This works because a successful regular-expression matching in the list context gives all the caps substrings, i.e. , $ 1 , $ 2 , $ 3 , and so on as appropriate.

    with a single pattern,

      qr / (\ d + (?: [-.] \ W +) *) (?:. * (SMP) )? /  

    The code above is 2.6.9-78.1.6.ELsmp SMP and 2.6.9-78.0.5.ELsmp in < Code> $ req_value . Capture filter for grep define ($ _) not taken subpotter. Without it, you get unsolicited price warnings for non-SMP case.

    The downside is that every regular expression should be reviewed so that all capturing groups should actually go to $ req_value . For example, the pattern of a person using

      qr / (xyz) OS (version \ d + | v- \ d +) /  

    As is so now, only XYZ will have to go to $ req_value , but the version number will be included using the above generalization if it is unwanted, then regular expression

     Do not capture  qr / (xyz) OS (?: Version \ d + | v- \ d +) /  

    (i.e., for the above pattern $ 2 does not produce Is): It's only for grouping.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -