Find case-insensitive word matches in a line
You need modifier /i
arr = line.scan(/\b#{word}\b/i)
http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm
And better to use \b
for word boundaries, because the second \s+
in your regex eats spaces, which may be used for the first \s+
of another matched word; also your regex fails on the beginning and the end of line:
> "asd asd asd asd".scan /\s+asd\s+/
=> [" asd "]
> "asd asd asd asd".scan /\basd\b/
=> ["asd", "asd", "asd", "asd"]
RegExp case insensitive search for whole word with variable
To check that a regex matches the entire string, you can use the assert beginning character (^
) and assert end ($
).
For example, hello
matches e
but not ^e$
.
For your code, just prepend ^
to the regex and append $
:
var re = new RegExp("^" + b + "$", "i");
fiddle
Edit: Some characters have special meanings in regexes (^
, $
, \
, .
, *
, etc). If you need to use any of these characters, they should be escaped with a \
. To do this, you can use this simple replace:
str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
So, your regex will end up being
new RegExp("^" + b.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + "$", "i");
See this question for more about escaping a regex.
You could also just convert the two strings to lowercase and then compare them directly. This will allow you to use special characters as well.
if (stringA.toLowerCase() == stringB.toLowerCase())) {
...
}
restrict 1 word as case sensitive and other as case insensitive in python regex | (pipe)
In Python 3.6 and later, you may use the inline modifier groups:
>>> s = "Welcome to PuNe, Maharashtra"
>>> print(re.findall(r"PuNe|(?i:MaHaRaShTrA)",s))
['PuNe', 'Maharashtra']
See the relevant Python re
documentation:
(?aiLmsux-imsx:...)
(Zero or more letters from the set'a'
,'i'
,'L'
,'m'
,'s'
,'u'
,'x'
, optionally followed by'-'
followed by one or more letters from the'i'
,'m'
,'s'
,'x'
.) The letters set or remove the corresponding flags:re.A
(ASCII-only matching),re.I
(ignore case),re.L
(locale dependent),re.M
(multi-line),re.S
(dot matches all),re.U
(Unicode matching), andre.X
(verbose), for the part of the expression. (The flags are described in Module Contents.)The letters
'a'
,'L'
and'u'
are mutually exclusive when used as inline flags, so they can’t be combined or follow'-'
. Instead, when one of them appears in an inline group, it overrides the matching mode in the enclosing group. In Unicode patterns(?a:...)
switches to ASCII-only matching, and(?u:...)
switches to Unicode matching (default). In byte pattern(?L:...)
switches to locale depending matching, and(?a:...)
switches to ASCII-only matching (default). This override is only in effect for the narrow inline group, and the
original matching mode is restored outside of the group.
New in version 3.6.
Changed in version 3.7: The letters
'a', 'L' and 'u' also can be used in a group.
Unfortunately, Python re
versions before 3.6 did not support these groups, nor did they support alternating on and off inline modifiers.
If you can use PyPi regex module, you may use a (?i:...)
construct:
import regex
s = "Welcome to PuNe, Maharashtra"
print(regex.findall(r"PuNe|(?i:MaHaRaShTrA)",s))
See the online Python demo.
How to do a case insensitive search in ed line editor
The ed regular expression syntax, being a sort of a POSIX regular expression standard, does not seem to support case insensitive modifiers.
You may use bracket expressions containing both upper- and lowercase letter variants:
[Pp][Rr][Oo][Pp]
where [Pp]
matches a p
or P
, etc.
If you plan to match prop
as a whole word, you will need to use \<
in front (it matches the beginning of a word) and a \>
at the end of the expression (it matches the end of a word).
Regex: match word that ends with “AM” (with case insensitive comparison)
You need to add case intensive flag at the end:
myinput.match(/^\w*am\b$/i)
Edit: as per your comment
The issue with 07.15am
is the first part of your regex. If you want to be strict then you could try:
/^(\w+)(\.\w+)\s?(am)$/i
This will match 07.15am
, 07.15Am
or 07.15 AM
.
If you want to be a little looser you can make the second group optional.
/^(\w+)(\.\w+)?\s?(am)$/i
Also this wont work if you have anything before or after the string. I would suggest removing the ^$ boundaries.
/(\w+)(\.\w+)?\s?(am)/i
That should cover most cases.
Regex - match whole line if has a specific word and case insensitive with PHP preg_match_all
This expression,
(?i)^.*\bworld\b.*$
might simply return those desired strings.
Test
$re = '/(?i)^.*\bworld\b.*$/m';
$str = 'this is my WoRld
this is my world
this is my wORLD
this is my home';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
var_dump($matches);
Output
array(3) {
[0]=>
array(1) {
[0]=>
string(16) "this is my WoRld"
}
[1]=>
array(1) {
[0]=>
string(16) "this is my world"
}
[2]=>
array(1) {
[0]=>
string(16) "this is my wORLD"
}
}
RegEx Circuit
jex.im visualizes regular expressions:
Related Topics
How to Round a Float to a Specified Number of Significant Digits in Ruby
How to Require File from 'Gem' Which Are Not Under 'Lib' Directory
You May Have Encountered a Bug in the Ruby Interpreter or Extension Libraries
How to Dynamically Define a Method as Private
Turn on Full Backtrace in Ruby on Rails Testcase
Man in the Middle (Mitm) Proxy with Https Support
Ruby Not Finding New Version of Openssl
Undefined Method Pluralize for Main:Object
Shorter Way to Pass Every Element of an Array to a Function
Rails Generate Error: No Such File or Directory - Getcwd
Is There a Pry Debug Setup That Works with Ruby 2.0
Thor & Yaml Outputting as Binary
What's the Purpose of Tainting Ruby Objects
Activeadmin Custom Views Which Retain the Activeadmin Layout
How to Tell Which Openssl Lib Is Actually Being Used by an Rvm-Installed Ruby