Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2024/25/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Extract data from a formatted string

See also:

(1) formattedRead()
from parse import parse

def main():
    s = "mul(11,8)"

    p = parse("mul({a},{b})", s)

    print(p["a"])  # "11"
    print(p["b"])  # "8"
import std.stdio;
import std.format;

void main()
{
    string s = "mul(11,8)";
    int a, b;
    auto items = formattedRead(s, "mul(%s,%s)", a, b);
    assert(items == 2, "couldn't extract both values");

    writeln(a); // 11
    writeln(b); // 8
    writeln(s); // "", empty string
    writeln(s.length); // 0
}

This is a 3rd-party library. You can install it with pip.

  • At the end, the string s is consumed, i.e. it becomes an empty string. If you want to keep the original string, call the function on a copy: formattedRead(s.dup, …)
  • The function returns the number of extracted elements. This way you can verify if everything went well.
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 June 27, 12:26