Mercurial Hosting > luan
comparison website/src/manual.html.luan @ 1722:7d2ab44f7a59
remove String regex fns
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 29 Jul 2022 14:12:01 -0600 |
parents | 5c69d2e8bd75 |
children | ba43135bb98d |
comparison
equal
deleted
inserted
replaced
1721:5c69d2e8bd75 | 1722:7d2ab44f7a59 |
---|---|
2483 Note that Java's <code>String.format</code> is too stupid to convert between ints and floats, so you must provide the right kind of number. | 2483 Note that Java's <code>String.format</code> is too stupid to convert between ints and floats, so you must provide the right kind of number. |
2484 </p> | 2484 </p> |
2485 <% | 2485 <% |
2486 end | 2486 end |
2487 } | 2487 } |
2488 ["String.gmatch"] = { | |
2489 title = "<code>String.gmatch (s, pattern)</code>" | |
2490 content = function() | |
2491 %> | |
2492 <p> | |
2493 Returns an iterator function that, | |
2494 each time it is called, | |
2495 returns the next captures from <code>pattern</code> (see <a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html">Pattern</a>) | |
2496 over the string <code>s</code>. | |
2497 If <code>pattern</code> specifies no captures, | |
2498 then the whole match is produced in each call. | |
2499 </p> | |
2500 | |
2501 <p> | |
2502 As an example, the following loop | |
2503 will iterate over all the words from string <code>s</code>, | |
2504 printing one per line: | |
2505 </p> | |
2506 <pre> | |
2507 local s = "hello world from Lua" | |
2508 for w in String.gmatch(s, [[\w+]]) do | |
2509 print(w) | |
2510 end | |
2511 </pre> | |
2512 | |
2513 <p> | |
2514 The next example collects all pairs <code>key=value</code> from the | |
2515 given string into a table: | |
2516 </p> | |
2517 <pre> | |
2518 local t = {} | |
2519 local s = "from=world, to=Lua" | |
2520 for k, v in String.gmatch(s, [[(\w+)=(\w+)]]) do | |
2521 t[k] = v | |
2522 end | |
2523 </pre> | |
2524 | |
2525 <p> | |
2526 For this function, a caret '<code>^</code>' at the start of a pattern does not | |
2527 work as an anchor, as this would prevent the iteration. | |
2528 </p> | |
2529 <% | |
2530 end | |
2531 } | |
2532 ["String.gsub"] = { | |
2533 title = "<code>String.gsub (s, pattern, repl [, n])</code>" | |
2534 content = function() | |
2535 %> | |
2536 <p> | |
2537 Returns a copy of <code>s</code> | |
2538 in which all (or the first <code>n</code>, if given) | |
2539 occurrences of the <code>pattern</code> (see <a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html">Pattern</a>) have been | |
2540 replaced by a replacement string specified by <code>repl</code>, | |
2541 which can be a string, a table, or a function. | |
2542 <code>gsub</code> also returns, as its second value, | |
2543 the total number of matches that occurred. | |
2544 The name <code>gsub</code> comes from <em>Global SUBstitution</em>. | |
2545 </p> | |
2546 | |
2547 <p> | |
2548 If <code>repl</code> is a string, then its value is used for replacement. | |
2549 The character <code>\</code> works as an escape character. | |
2550 Any sequence in <code>repl</code> of the form <code>$<em>d</em></code>, | |
2551 with <em>d</em> between 1 and 9, | |
2552 stands for the value of the <em>d</em>-th captured substring. | |
2553 The sequence <code>$0</code> stands for the whole match. | |
2554 </p> | |
2555 | |
2556 <p> | |
2557 If <code>repl</code> is a table, then the table is queried for every match, | |
2558 using the first capture as the key. | |
2559 </p> | |
2560 | |
2561 <p> | |
2562 If <code>repl</code> is a function, then this function is called every time a | |
2563 match occurs, with all captured substrings passed as arguments, | |
2564 in order. | |
2565 </p> | |
2566 | |
2567 <p> | |
2568 In any case, | |
2569 if the pattern specifies no captures, | |
2570 then it behaves as if the whole pattern was inside a capture. | |
2571 </p> | |
2572 | |
2573 <p> | |
2574 If the value returned by the table query or by the function call | |
2575 is not <b>nil</b>, | |
2576 then it is used as the replacement string; | |
2577 otherwise, if it is <b>nil</b>, | |
2578 then there is no replacement | |
2579 (that is, the original match is kept in the string). | |
2580 </p> | |
2581 | |
2582 <p> | |
2583 Here are some examples: | |
2584 </p> | |
2585 <pre> | |
2586 x = String.gsub("hello world", [[(\w+)]], "$1 $1") | |
2587 --> x="hello hello world world" | |
2588 | |
2589 x = String.gsub("hello world", [[\w+]], "$0 $0", 1) | |
2590 --> x="hello hello world" | |
2591 | |
2592 x = String.gsub("hello world from Luan", [[(\w+)\s*(\w+)]], "$2 $1") | |
2593 --> x="world hello Luan from" | |
2594 | |
2595 x = String.gsub("4+5 = $return 4+5$", [[\$(.*?)\$]], function (s) | |
2596 return load(s)() | |
2597 end) | |
2598 --> x="4+5 = 9" | |
2599 | |
2600 local t = {name="lua", version="5.3"} | |
2601 x = String.gsub("$name-$version.tar.gz", [[\$(\w+)]], t) | |
2602 --> x="lua-5.3.tar.gz" | |
2603 </pre> | |
2604 <% | |
2605 end | |
2606 } | |
2607 ["String.lower"] = { | 2488 ["String.lower"] = { |
2608 title = "<code>String.lower (s)</code>" | 2489 title = "<code>String.lower (s)</code>" |
2609 content = function() | 2490 content = function() |
2610 %> | 2491 %> |
2611 <p> | 2492 <p> |
2612 Receives a string and returns a copy of this string with all | 2493 Receives a string and returns a copy of this string with all |
2613 uppercase letters changed to lowercase. | 2494 uppercase letters changed to lowercase. |
2614 All other characters are left unchanged. | 2495 All other characters are left unchanged. |
2615 </p> | 2496 </p> |
2616 <% | |
2617 end | |
2618 } | |
2619 ["String.match"] = { | |
2620 title = "<code>String.match (s, pattern [, init])</code>" | |
2621 content = function() | |
2622 %> | |
2623 <p> | |
2624 Looks for the first <em>match</em> of | |
2625 <code>pattern</code> (see <a href="http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html">Pattern</a>) in the string <code>s</code>. | |
2626 If it finds one, then <code>match</code> returns | |
2627 the captures from the pattern; | |
2628 otherwise it returns <b>nil</b>. | |
2629 If <code>pattern</code> specifies no captures, | |
2630 then the whole match is returned. | |
2631 A third, optional numerical argument <code>init</code> specifies | |
2632 where to start the search; | |
2633 its default value is 1 and can be negative. | |
2634 </p> | |
2635 <% | |
2636 end | |
2637 } | |
2638 ["String.matches"] = { | |
2639 title = "<code>String.matches (s, pattern)</code>" | |
2640 content = function() | |
2641 %> | |
2642 <p> | |
2643 Returns a boolean indicating whether the <code>pattern</code> can be found in string <code>s</code>. | |
2644 This function is equivalent to | |
2645 </p> | |
2646 <pre> | |
2647 return String.match(s,pattern) ~= nil | |
2648 </pre> | |
2649 <% | 2497 <% |
2650 end | 2498 end |
2651 } | 2499 } |
2652 ["String.regex"] = { | 2500 ["String.regex"] = { |
2653 title = "<code>String.regex (s)</code>" | 2501 title = "<code>String.regex (s)</code>" |