Prettier auto "correct" regex escaping forward slash `\` [duplicate] - regex

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 5 years ago.
pattern: '^131\.[0-9]{6}$',
prettier change it to pattern: '^131.[0-9]{6}$',. Is there a way to ignore line, or ignore file?

Assuming JavaScript (as you're using prettier.) The '^131\.[0-9]{6}$' is just a string, not a regex. Prettier removes unnecessary escape characters when reformatting. As \. isn't a meaningful escape, it's the same as just having . on its own in string context.
Your aim is to get \. into a regex, which I assume you're going to create using the new RegExp() constructor; in that case you want to escape the backslash:
pattern: '^131\\.[0-9]{6}$'

Related

How to exclude a substring in a regular expression? [duplicate]

This question already has answers here:
What is the difference between .*? and .* regular expressions?
(3 answers)
What do 'lazy' and 'greedy' mean in the context of regular expressions?
(13 answers)
Closed 5 months ago.
There is a line of text:
Lorem ~Ipsum~ is simply ~dummy~ text ~of~ the printing...
To find all the words enclosed in ~~ I use
re.search(r'~([^~]*)~', text)
Let's say it became necessary to use ~~ instead of ~
([^\~]+) indicates to exclude the ~ character from the text within those characters
How do I make a regular expression to exclude a string of characters instead of just one?
That is, ~~Lor~em~~ should return Lor~em
The symbol of the new string must not be excluded and the length of the found string cannot be 0
Use a non-greedy quantifier instead of a negated character set.
re.search(r'~~(.*?)~~', text, flags=re.DOTALL)
re.DOTALL makes . match newline characters.

Parsing regex with escaped pipe delimiter [duplicate]

This question already has answers here:
regular expression to match pipe separated strings with pipe escaping
(4 answers)
Closed 3 years ago.
Im trying to parse
|123|create|item|1497359166334|Sport|Some League|\|Team\| vs \|Team\||1497359216693|
With regex (https://regex101.com/r/KLzIOa/1/)
I currently have
[^|]++
Which is parsing everything correctly except \|Team\| vs \|Team\|
I would expect this to be parsed as |Team| vs |Team|
If i change the regex to
[^\\|]++
It parses the Teams separately instead of together with the escaped pipe
Basically i want to parse the fields between the pipes however, if there are any escaped pipes i would like to capture them. So with my example i would expect
["123", "create", "item", "1497359166334", "Sport", "Some League", "|Team| vs |Team|", "1497359216693"]
You can alternate between:
\\. - A literal backslash followed by anything, or
[^|\\]+ - Anything but a pipe or backslash
(?:\\.|[^|\\]+)+
https://regex101.com/r/KLzIOa/2
Note that there's no need for the possessive quantifier, because no backtracking will occur.
If you also want to replace \|s with |s, then do that afterwards: match \\\| and replace with |.
To handle escaping, you should match a backslash and the character after it as a single "item".
(?:\\.|[^|])++
This conveniently also works for escaping the backslashes themselves!
To then remove the backslashes from the results, use a simple replacement:
Replace: \\(.)
With: $1
Use:
(?:\\\||[^|])+
Demo & explanation

Regex Quiz. Quoted text with escapes [duplicate]

This question already has answers here:
Regex for quoted string with escaping quotes
(16 answers)
Closed 3 years ago.
Quiz:
Validate a line in quotes. Return one (and only one) backreference
with the text. ie: quoted text from "quoted text". Note: a \ escapes
any char, so \" is a valid escape.
I know it's a common question and already been answered lots of times, but the problem is that no answer fits the correct for this quiz.
I started from simple (empty quotes are valid, should return null value in the backreference):
(?<=\s|^)"(.*?[^\\])?"(?=\s|$)
It shouldn't match "text"with quote". Only escaped quotes are allowed
inside the main (opening and closing) quotes. You could use [^"].
Tried with backreferencing
((?<![\\])['"])((?:.(?!(?<![\\])\1))*.?)\1
You're using more than 1 group. Please only allow your pattern to set
one backreference.
This one was accepted by quiz
^"([^"\\]*(?:\\.[^"\\]*)*)"$

Quite confused about `\?` in vim's regex [duplicate]

This question already has answers here:
How can I make my match non greedy in vim?
(8 answers)
Closed 4 years ago.
I'we been trying to do simple substitution in vim, and find out that the \? in vim not works with * or +, saying that (NFA regexp) Can't have a multi follow a multi, in the vim:
i want it to stop here, not here
~
~
~
[NORMAL] ...
:%s/^\(.*\?\)here//
If I remove \? it works, but the it regex matches up to 2nd here.
But with normal regex it works: https://regex101.com/r/iHdxxl/1
Why it isn't possible to use \? with * or \+ in vim?
As stated there, you can't add the ? char in vim after the asterisk.
To make the search non greedy, you need to use .\{-} instead of .*:
:%s/\(.\{-}\)here//
Another option is to use negative lookahead:
:%s/\v^((here)#!.)* here//
\v is used for very magic to avoid escaping all over in regex.

Add spacebar in regular expression [duplicate]

This question already has answers here:
Matching a space in regex
(10 answers)
Closed 9 years ago.
I use this regular expression for checking
public const string FullNameRegularExpression = #"^[a-zA-Z0-9._-]+$";
How to add "spacebar" in?
If you are looking for one single space it is: (" "), a very complete example can be found in this reference.
Or if you want to match any whitespace character (\n,\r,\f,\t, ), you can use \s.
Notice an added \s
public const string FullNameRegularExpression = #"^[a-zA-Z0-9._-\s]+$";
You may push a spacebar on your keyboard or add \s or \s+ or \s* to your regex ;-)