This should work :
str = str.replace(/[^a-z0-9-]/g, '');
Everything between the indicates what your are looking for
/is here to delimit your pattern so you have one to start and one to end[]indicates the pattern your are looking for on one specific character^indicates that you want every character NOT corresponding to what followsa-zmatches any character between 'a' and 'z' included0-9matches any digit between '0' and '9' included (meaning any digit)-the '-' charactergat the end is a special parameter saying that you do not want you regex to stop on the first character matching your pattern but to continue on the whole string
Then your expression is delimited by / before and after.
So here you say "every character not being a letter, a digit or a '-' will be removed from the string".
This should work :
str = str.replace(/[^a-z0-9-]/g, '');
Everything between the indicates what your are looking for
/is here to delimit your pattern so you have one to start and one to end[]indicates the pattern your are looking for on one specific character^indicates that you want every character NOT corresponding to what followsa-zmatches any character between 'a' and 'z' included0-9matches any digit between '0' and '9' included (meaning any digit)-the '-' charactergat the end is a special parameter saying that you do not want you regex to stop on the first character matching your pattern but to continue on the whole string
Then your expression is delimited by / before and after.
So here you say "every character not being a letter, a digit or a '-' will be removed from the string".
Just change + to -:
str = str.replace(/[^a-z0-9-]/g, "");
You can read it as:
[^ ]: match NOT from the set[^a-z0-9-]: match if nota-z,0-9or-/ /g: do global match
More information:
- https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
How Replace a string in regex
I'm somewhat of a coder myself
In your case there are a couple of easy solutions. This first solution uses only basic regex:
[\.\w]+$
This captures every word character \w or period \. from the end $, stopping when it reaches any other type of character. This works because the space is not a word character or a period.
If you would like to select the region you want to remove, you can just use:
.*
Read this as match every character .* until the last space . The reason this matches until the last space, is because the star is a greedy quantifier. This means it matches everything it can, then starts giving back characters as needed for the rest of the pattern to match. To match until the first space, use .*? . The question mark next to the star makes the star lazy. This means the star will match as little as it has to in order for the next part of the expression to find a match.
However, capture groups are ideal for problems like this, where you would like to remove part of a line and keep the rest. A simple formula for this is:
^(.*)pattern_to_remove(.*)$
You can then recover the rest using backreferences. These store the capture groups (anything in parenthesis) from you regex into preset variables. In the pattern above, this would be \1\2 or depending on the language you are using. This brings me to probably the easiest answer to your problem:2
(.*)
The first capture group contains everything after the space. This regex is very straightforward and easy to read. Move to the first space , then capture everything after (.*).
For a comprehensive reference on regex, check out regular-expressions.info. Here is a link to the page on capture groups.
- Ctrl+H
- Find what:
^\S+\h+ - Replace with:
LEAVE EMPTY - CHECK Wrap around
- CHECK Regular expression
- Replace all
Explanation:
^ # beginning of line
\S+ # 1 or more non spaces
\h+ # 1 or more horizontal spaces
Screenshot (before):

Screenshot (after):

This tested snippet should do it:
import re
line = re.sub(r"</?\[\d+>", "", line)
Edit: Here's a commented version explaining how it works:
line = re.sub(r"""
(?x) # Use free-spacing mode.
< # Match a literal '<'
/? # Optionally match a '/'
\[ # Match a literal '['
\d+ # Match one or more digits
> # Match a literal '>'
""", "", line)
Regexes are fun! But I would strongly recommend spending an hour or two studying the basics. For starters, you need to learn which characters are special: "metacharacters" which need to be escaped (i.e. with a backslash placed in front - and the rules are different inside and outside character classes.) There is an excellent online tutorial at: www.regular-expressions.info. The time you spend there will pay for itself many times over. Happy regexing!
str.replace() does fixed replacements. Use re.sub() instead.