Check for only one specific special character in string - full-text-search

I want to check if a string contains only '*' and treat replace it with blank.
For eg:
Scenario 1: if the string contains '**abc*%#' or 'xyz*$#*!' then I need to retain their values as is.
Scenario 2: if the string contains values like '****' or '*' or any combination where only this special character is present in the string then I need an output of ''.
Please suggest what would be the best way to go about this.
Thanks.

You can achieve this by getting the length of the string and replacing the * with '' and then subtract both of them. If the length is 0 then it contains all *
Example
int count = line.length() - line.replace(".", "").length();
Hope this helps.
-Help :)

Related

Split a string and remove the first element in string

Original string '4.0.0-4.0-M-672092'
How to modify the Original string to "4.0-M-672092" using a one line code.
Any Help is highly appreciated .
Thanks and Regards
The 'split' method works in this case
https://apidock.com/ruby/String/split
'4.0.0-4.0-M-672092'.split('-')[1..-1].join('-')
# => "4.0-M-672092"
Just be careful, in this application is fine, but in long texts this might become unoptimized, since it splits all the string and then joins the array all over again
If you need this in wider texts to be more optimized, you can find the "-" index (which is your split) and use the next position to make a substring
text = '4.0.0-4.0-M-672092'
text[(text.index('-') + 1)..-1]
# => "4.0-M-672092"
But you can't do it in one line, and not finding a split character will result in an error, so use a rescue statement if that is possible to happen
Simplest way:
'4.0.0-4.0-M-672092'.split('-', 2).second
"4.0.0-4.0-M-672092"[/(?<=-).*/]
#=> "4.0-M-672092"
The regular expression reads, "Match zero or more characters other than newlines, as many as possible (.*), provided the match is preceded by a hyphen. (?<=-) is a positive lookbehind. See String#[].

How to Convert a string without any delimiter to a comma delimited string?

I have a file details.txt in which data stored is in this format
"571955NandhithaF1975-12-222011-12-06Mumbai"
Columns are first six digit unique id ,
name , (M/F) Gender , dob,joining date , and location
i have to separate this in six columns using comma delimiter !!
Please help me in this problem
Pass each line into a regex function which contains the below logic :
String expression = "571955NandhithaF1975-12-222011-12-06Mumbai";
Pattern pattern = Pattern
.compile("([0-9]{6})([a-zA-Z]+)([M|F])([0-9]{4}-[0-9]{2}-[0-9]{2})([0-9]{4}-[0-9]{2}-[0-9]{2})([a-zA-Z0-9]+)");
Matcher matcher = pattern.matcher(expression);
if (matcher.find()) {
//System.out.println(matcher.group());
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
System.out.println(matcher.group(3));
System.out.println(matcher.group(4));
System.out.println(matcher.group(5));
System.out.println(matcher.group(6));
}
output:
571955
Nandhitha
F
1975-12-22
2011-12-06
Mumbai
571955NandhithaF1975-12-222011-12-06Mumbai
To split this type of data, we have to use String Functions in java in the mapper class under map method.
You can use substring(beginindex,endindex) method to get Id of from the string, its
like string id[6]=substring(0,5) which returns 6 digit string that is ID.(As ID is Fixed length we take 6)
You can use substring(beginindex) to get remaining string.
Next on wards you have to use REGXP in java.. along with split(regexp) to get the name, gender, dob, doj, loc. But definitely some workout with java takes place.
go through this link for String functions in java.
Hope this post may help.
If any suggestions or modifications to the same are also accepted :)

string capture between duplicates in ruby

string = 'xabcdexfghijk'
In the example above, 'x' appears twice. I want to capture everything between the first 'x' and the next 'x'. Thus, the desired result is a new string that equals 'xabcdex'. Any ideas?
You could use a simple regular expression: /x.*?x/. This basically means "match any characters in between two x characters, as few times as possible (non-greedy)".
The matched text can be extracted with String#[regexp]
string = 'xabcdexfghijk'
string[/x.*?x/] # => "xabcdex"

Remove email address from string in Ruby

I have the following code which is supposed to be removing a particular email address from a string if it exists. The problem is i get the error "invalid range "y-d" in string transliteration (ArgumentError)" which I assume is because it's treating my input as a regex. I will need to do this delete by a variable in the actual code, not a string literal but this is a simplified version of the problem.
So how do I properly perform this operation?
myvar = "test1#my-domain.com test2#my-domain.com"
myvar = myvar.delete("test1#my-domain.com")
Try
myvar = "test1#my-domain.com test2#my-domain.com"
myvar = myvar.gsub("test1#my-domain.com", '').strip
String#delete(str) does not delete the literal string str but builds a set out of individual characters of str and deletes all occurrences of these characters. try this:
"sets".delete("test")
=> ""
"sets".delete("est")
=> ""
The hyphen has a special meaning, it defines a range of characters. String#delete("a-d") will delete all occurrences of a,b,c and d characters. Range boundary characters should be given in ascending order: you should write "a-d" but not "d-a".
In your original example, ruby tries to build a character range from y-d substring and fails.
Use String#gsub method instead.
You can do it like this
myvar = "test1#my-domain.com test2#my-domain.com"
remove = "test1#my-domain.com"
myvar.gsub!(remove, "")

Regexp, how to limit a match

I have a string:
string = %q{<span class="no">2503</span>read_attribute_before_type_cast(<span class="pc">self</span>.class.primary_key)}
In this example I want to match the words 'class' which are not in the tag. Regexp for this:
/\bclass[^=]/
But the problem is that it matches the last letter
/\bclass[^=]/.match(string) => 'class.'
I don't want have a last dot in a result. I've tried this regexp:
/\bclass(?:[^=])/
but still got the same result. How to limit the result to 'class'? Thanks
You are almost correct, but you have an error in your look ahead. Try this:
/\bclass(?!=)/
The regex term (?!=) means the input to the right must not match the character '='
You can take your variable string and extract a subsection using groups:
substring = string[/\b(class)[^=]/, 1]
The brackets around class will set that as the first "group", which is referred to by the 1 as the second parameter in the square brackets.
Assuming your only issue is keeping it from matching span.class.blah, just ignore . as well, so [^=.].

Resources