Check that a string exists within another string with regular expression - ruby

I've got a regular expression that I am using to strip an extension from another string
The extensions in this example are
BK|BZ|113
If the string does not contain any of the extensions, then I need it to leave the string as is.
The regular expression I'm using is
base_value = base_string[/(.*?[^-])-?(?:BK|BZ|113)/,1]
However, if the base_value does not contain the string, I want to return the base_value. I thought I could use
base_value = base_string
base_value = base_string[/(.*?[^-])-?(?:BK|BZ|113)/,1] unless (base_value !~ /(.*?[^-])-?(?:BK|BZ|113)) == false
but that isn't working.
What is the best way to return the base_string if the extensions are not found?

If I understand well, you want to strip the regexp from a string, if it matches, right? So.. do it:
string.sub(/(?:BK|BZ|113)$/, "")

Perhaps you can use sub like this:
base_string1 = "test1BK"
base_string2 = "test2"
p base_string1.sub(/(.*?[^-])-?(?:BK|BZ|113)/,'\1')
p base_string2.sub(/(.*?[^-])-?(?:BK|BZ|113)/,'\1')
When the pattern is not found nothing is replaced, otherwise it returns the string without the extension.

If /(.*?[^-])-?(?:BK|BZ|113)/ doesn't match then base_string[/(.*?[^-])-?(?:BK|BZ|113)/,1] will be nil. So this should work:
base_value = base_string[/(.*?[^-])-?(?:BK|BZ|113)/,1] || base_string

Related

How to check if a string contains each regex

I would like to check if a string includes all of the given regexs. I dont want to go through the string for each regex.
return "foo".all_chars {include? ( \letter\ && \number\ && \special\)}
or
return "foo".all_chars {include? ( \letter , number , special\)}
I dont want to go through the string for each regex.
You'd end up with a really nasty, unreadable, and unmaintainable regex pattern. But if you want to you could combine them, and then just call match? on it.
Given...
regex1 = %r{[a-z]}
regex2 = %r{[^aeiou]}
all_regex = %r{[a-z&&[^aeiou]]}
But here's really no harm in using Enumerable#all?
[regex1, regex2, regex3].all? { |regex| string.match?(regex) }

Get a string using regular expressin in ruby

I have some strings like this
../..//somestring
../somestring
../..somestring
./somestring
How to write a regular expression in ruby to extract "somestring" from above strings. Before some strings it can be any combination of . and /
Thanks for you help
Do this:
string.sub(/\A[.\/]+/, "")
"../../test/file/cases".sub(/\A[.\/]+/, "")
# => "test/file/cases"
Just match letters:
str = "../..//somestring" # or "../somestring", "../..somestring", "./somestring"
str[/[a-z]+/] # somestring
RE: your comment
If you just want to remove leading dots and slashes, use
str.gsub(/[.\/]/, '')
It looks like you're dealing with file paths. If so, there are more appropriate tools than regexps
File.basename('../..//somestring')
# "somestring"
regexp = /^[.\/]+(.*?)$/i
result = subject.scan(regexp)
http://rubular.com/r/tMYKPe78uc

Using regex to find an exact pattern match in Ruby

How would I go about testing for an exact match using regex.
"car".match(/[ca]+/) returns true.
How would I get the above statement to return false since the regex pattern doesn't contain an "r"? Any string that contains any characters other than "c" and "a" should return false.
"acacaccc" should return true
"acacacxcc" should return false
Add some anchors to it:
/^[ca]+$/
You just need anchors.
"car".match(/^[ca]+$/)
This'll force the entire string to be composed of "c" or "a", since the "^" and "$" mean "start" and "end" of the string. Without them, the regex will succeed as long as it matches any portion of the string.
Turn your logic around and look for bad things:
string.match(/[^ca]/)
string.index(/[^ca]/)
If either of the above are non-nil, then you have a bad string. If you just want to test and don't care about where it matches then:
if string.index(/[^ca]/).nil?
# You have a good string
else
# You have a bad string
For example:
>> "car".index(/[^ca]/).nil?
=> false
>> "caaaacaac".index(/[^ca]/).nil?
=> true
try this
"car".match /^(a|c)+$/
Try this:
"car".match(/^(?:c|a)$/)

Regex to leave desired string remaining and others removed

In Ruby, what regex will strip out all but a desired string if present in the containing string? I know about /[^abc]/ for characters, but what about strings?
Say I have the string "group=4&type_ids[]=2&type_ids[]=7&saved=1" and want to retain the pattern group=\d, if it is present in the string using only a regex?
Currently, I am splitting on & and then doing a select with matching condition =~ /group=\d/ on the resulting enumerable collection. It works fine, but I'd like to know the regex to do this more directly.
Simply:
part = str[/group=\d+/]
If you want only the numbers, then:
group_str = str[/group=(\d+)/,1]
If you want only the numbers as an integer, then:
group_num = str[/group=(\d+)/,1].to_i
Warning: String#[] will return nil if no match occurs, and blindly calling nil.to_i always returns 0.
You can try:
$str =~ s/.*(group=\d+).*/\1/;
Typically I wouldn't really worry too much about a complex regex. Simply break the string down into smaller parts and it becomes easier:
asdf = "group=4&type_ids[]=2&type_ids[]=7&saved=1"
asdf.split('&').select{ |q| q['group'] } # => ["group=4"]
Otherwise, you can use regex a bunch of different ways. Here's two ways I tend to use:
asdf.scan(/group=\d+/) # => ["group=4"]
asdf[/(group=\d+)/, 1] # => "group=4"
Try:
str.match(/group=\d+/)[0]

Simple Ruby Regex Question

I have a string in Ruby:
str = "<TAG1>Text 1<TAG1>Text 2"
I want to use gsub to get a string like this:
want = "<TAG2>Text 1</TAG2><TAG2>Text2</TAG2>"
In other words, I want to save everything in between a <TAG1> and EITHER: 1) the next occurrence of a "<", or 2) the end of the string.
The best regex i could come up with was:
regex = /<TAG1>(.*)(?:<|$)/
But the problem with this is that it'll just match the entire str, where what I want is both matches within str. (In other words, it seems like the end of string char ($) seems to have precedence over the "<" character--is there a way to flip it around?
/<TAG1>([^<]*)/ will match that. If there's no < it'll go all the way to the end of the string. Otherwise it will stop when it hits a <. Your problem is that . matches < as well. An alternative way would be to do /<TAG1>(.*?)(?:<|$)/, which makes the * non-greedy.

Resources