PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 4, 2022

[FIXED] How to replace characters based on variables in a list using regex sub

 November 04, 2022     lambda, python-3.x, regex, regex-group     No comments   

Issue

Is it possible to insert different values in a string based on the same matching pattern by using a list to specify the values to insert? I'm using python 3 and re.sub.

Here is what a mean:

line = "Float: __, string: __"
values = ["float", "string"]

newLine = re.sub('(_+)([.]_+)?',
    (lambda m:
    f'%'
    f'{len(m.group(1))}'
    f'{"." if values == "float" else ""}'
    f'{(len(m.group(2) or ".")-1) if values == "float" else ""}'
    f'{"f" if values == "float" else "s"}'),
    line)

print(newLine)                                                

Expected result:

Float: %2.0f, string: %2s

Actual result:

Float: %2s, string: %2s

Is it posssible to loop through the values list in order to get the correct result (this is not working and I get a TypeError)? Something like:

newLine = re.sub('(_+)([.]_+)?',
    ((lambda m:
    f'%'
    f'{len(m.group(1))}'
    f'{"." if v == "float" else ""}'
    f'{(len(m.group(2) or ".")-1) if v == "float" else ""}'
    f'{"f" if v == "float" else "s"}') for v in values),
    line)

Edit 1 The values list and the amount of matching patterns are always of same length.


Solution

You cannot loop like this inside the repl argument, but you can define a counter and pass it to the repl function:

import re

line = "Float: __, string: __"
values = ["float", "string"]
obj = lambda:None
obj.i = -1

def repl(m, count, values):
    count.i += 1
    return f'%{len(m.group(1))}{"." if values[count.i] == "float" else ""}{(len(m.group(2) or ".")-1) if values[count.i] == "float" else ""}{"f" if values[count.i] == "float" else "s"}'

newLine = re.sub(r'(_+)(\._+)?',
    lambda m: repl(m, obj, values),
    line)

print(newLine)      
## => Float: %2.0f, string: %2s

See the Python demo.

Here, an obj object is declared, and its i property is initialized with -1. Then, inside the repl function, its value is incremented and used as the index of the values list.

Note that this approach works only if the number of matches equals the amount of items in the values list.



Answered By - Wiktor Stribiżew
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,206,305

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing