[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/Lovepython015/Python/master/Article/PythonBasis/python14/5.md [Back]  [Original]

# re.sub

 `def sub(pattern, repl, string, count=0, flags=0)  ` re.sub pattern, repl, string ; count, flags .



|     |                                                           |
| ------- | ------------------------------------------------------------- |
| pattern |                                         |
| repl    | replreplacement                   |
| string  |  string                   |
| count   | patterncountgroup |
| flags   |                                               |

 `convert` ,

```python
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import re

a = 'Python*Android*Java-888'

#  *  & 
sub1 = re.sub('\*', '&', a)
print(sub1)

#  *  & 
sub2 = re.sub('\*', '&', a, 1)
print(sub2)


#  *  & , -  |

# 1
def convert(value):
    group = value.group()
    if (group == '*'):
        return '&'
    elif (group == '-'):
        return '|'


# 
sub3 = re.sub('[\*-]', convert, a)
print(sub3)
```



```txt
Python&Android&Java-888
Python&Android*Java-888
Python&Android&Java|888
```

Web Proxy Viewer  |  New URL  |  Original Page