Asked  7 Months ago    Answers:  5   Viewed   26 times

How can I make the following regex ignore case sensitivity? It should match all the correct characters but ignore whether they are lower or uppercase.

G[a-b].*

 Answers

24

Assuming you want the whole regex to ignore case, you should look for the i flag. Nearly all regex engines support it:

/G[a-b].*/i

string.match("G[a-b].*", "i")

Check the documentation for your language/platform/tool to find how the matching modes are specified.

If you want only part of the regex to be case insensitive (as my original answer presumed), then you have two options:

  1. Use the (?i) and [optionally] (?-i) mode modifiers:

    (?i)G[a-b](?-i).*
    
  2. Put all the variations (i.e. lowercase and uppercase) in the regex - useful if mode modifiers are not supported:

    [gG][a-bA-B].*
    

One last note: if you're dealing with Unicode characters besides ASCII, check whether or not your regex engine properly supports them.

Tuesday, June 1, 2021
 
Freddie
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
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
47

Flags go at the end.

/regex/i

i is for case-Insensitive (or ignore-case)

Saturday, July 3, 2021
 
antoniputra
answered 6 Months ago
83

The short/simple answer: case-sensitivity varies from standard to standard, and the particular implementations you were using have case-sensitive identifiers. But this information is probably not very helpful in isolation. So what went "wrong"?

Given that you mention #langs, it's clear you were using Racket. Racket is a descendant of Scheme, but it does not conform to any existing Scheme standard. However, Racket is extensible—it can support lots of languages, even user-defined ones—so different iterations of Scheme are supported by Racket.

Which Schemes are case-sensitive?

There are three relevant versions of the Scheme standard in existence at the time of this writing, R5RS, R6RS, and R7RS.

  • R5RS is always case-insensitive.
  • R6RS is always case-sensitive.
  • R7RS is case-sensitive by default, but the #!fold-case directive or the include-ci form can disable case-sensitivity.

As you can see, this issue really isn't clear-cut: it hasn't remained consistent over the past three standards. In fact, given that R7RS and R6RS are both case-sensitive by default, it might be more accurate to say that Scheme is case-sensitive now. However, while R5RS has been outdated for a long time, it continues to live on as "The Scheme" used in many books, interpreters and compilers, and other materials, hence the possible conclusion that "Scheme is case-insensitive."

Which Scheme were you using?

None of them. Perhaps confusingly, even #lang scheme isn't an implementation of any Scheme standard. In truth, #lang scheme is a deprecated language that has been completely supplanted by #lang racket (the former existed before PLT Scheme was renamed to Racket, much in part to reduce the confusion about its "Scheme" not being an actual Scheme implementation).

So, you may ask, how can you get actual Scheme inside of Racket? Well, Racket does provide implementations of the various Scheme standards. Both #lang r5rs and #lang r6rs are built-in implementations of the relevant Scheme standards. An R7RS implementation is available as #lang r7rs through the r7rs package.

All of Racket's languages can interoperate, so your language of choice is up to you, though the RnRS languages tend to go unused in the Racket community (#lang racket is much more useful for writing programs than any of the Scheme implementations), but they can be useful if you want to write programs that run on different Scheme implementations.

Sunday, November 14, 2021
 
jdehesa
answered 4 Weeks 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