Asked  7 Months ago    Answers:  5   Viewed   7 times

I'm using the following regex

^[a-zA-Z0-9',!;?$^:\/`|~&" @#%*{}()_+.s=-]{1,1000}$

I know it's ugly, but so far it serves its purpose other than the backslash not being allowed as I think it should because it's escaped, I also tried \ instead of \ but same results. Any ideas?

 Answers

40

If you're putting this in a string within a program, you may actually need to use four backslashes (because the string parser will remove two of them when "de-escaping" it for the string, and then the regex needs two for an escaped regex backslash).

For instance:

regex("\\")

is interpreted as...

regex("\" [escaped backslash] followed by "\" [escaped backslash])

is interpreted as...

regex(\)

is interpreted as a regex that matches a single backslash.


Depending on the language, you might be able to use a different form of quoting that doesn't parse escape sequences to avoid having to use as many - for instance, in Python:

re.compile(r'\')

The r in front of the quotes makes it a raw string which doesn't parse backslash escapes.

Tuesday, June 1, 2021
 
tika
answered 7 Months ago
43

Continue calling re.exec(s) in a loop to obtain all the matches:

var re = /s*([^[:]+):"([^"]+)"/g;
var s = '[description:"aoeu" uuid:"123sth"]';
var m;

do {
    m = re.exec(s);
    if (m) {
        console.log(m[1], m[2]);
    }
} while (m);

Try it with this JSFiddle: https://jsfiddle.net/7yS2V/

Tuesday, June 1, 2021
 
barden
answered 7 Months ago
86

Use a double slash in Java string literal to escape a slash :

String s = "c:\new folder\title.csv";

If an end user enters a string in a JFileChooser, the string variable will contain all the characters entered by the user. Escaping is only needed when using String literals in Java source code.

And use a prepared statement to insert strings into a database table. This will properly escape special characters and avoid SQL injection attacks. Read more about prepared statements in the Java tutorial about JDBC.

Friday, June 11, 2021
 
mattltm
answered 6 Months ago
28

If you merely want to check whether a regular expression is valid or not, simply try to compile it with whichever programming language or regular expression library you're working with.

Parsing regular expressions is far from trivial. As the author of RegexBuddy, I have been around that block a few times. If you really want to do it, use a regex to tokenize the input, and leave the parsing logic to procedural code. That is, your regex would match one regex token (^, $, w, (, ), etc.) at a time, and your procedural code would check if they're in the right order.

Thursday, July 1, 2021
 
Sufi
answered 6 Months ago
31

There are other problems with your script, but file names containing $ are not a problem if you properly quote the argument to rm in the resulting script.

echo "rm -f '$i'" >> REMOVEOLDFILES.sh

or using printf, which makes quoting a little nicer and is more portable:

printf "rm -f '%s'" "$i" >> REMOVEOLDFILES.sh

(Note that I'm addressing the real problem, not necessarily the question you asked.)

Monday, August 16, 2021
 
IronGeek
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share