Hello,
I have the following strings samples. I need a query to return 7th and 8th
character in a string.
600110710180 -- the result is 71
600200487090 -- the result is 48
604500820033 -- the result is 82
Thank you for your help.
You're probably looking for :
SUBSTRING(stringSample, startPosition, length);
Where "stringSample" is the string you're working on, "startPosition" is the position where you start your character extraction, and "length" is the number of characters you want to extract.
So here, in your case :
SUBSTRING(600110710180,7,2);
Related
I want to create a Regular expression to ignore each first and second word of a selected sentence
For example I have this phrase "October 27 New Store Products / October 2022". I want to create a regex that will choose only this part of the phrase ~ "New Store Products / October 2022" and ignore the first date part of the phrase ~ "October 27".
Without knowledge of your true requirements, all we can do is provide best guess, so here is mine;
What you could do, is have something such as the following;
/^\S+\s+\S+\s+(.*)$/
What this would do is the following;
From the beginning of the string (^), find one or more non-whitespace chars (\S+), find one or more whitespace chars (\s+) - repeat this again and then use a capture group ((.*)) to get everything else until the end of the string ($).
If you are using JavaScript, you could use this as such;
let sentence = "October 27 New Store Products / October 2022";
let regex = /^\S+\s+\S+\s+(.*)$/;
let match = regex.exec(sentence);
if (match) {
// Ignores the first and second words of the sentence
console.log(match[1]); // Output: "New Store Products / October 2022" ignoring "October 27"
}
Further explanation of this regex taken from regex1011 when this is put into the regex bar
/^\S+\s+\S+\s+(.*)$/
^ asserts position at start of the string
\S matches any non-whitespace character (equivalent to [^\r\n\t\f\v ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\S matches any non-whitespace character (equivalent to [^\r\n\t\f\v ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
1st Capturing Group (.*)
. matches any character (except for line terminators)
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
1 Emphasis mine
You've not provided any information on the context, but does it need to be a regular expression?
String manipulation by searching on spaces might be easier.
For example in PHP:
$string = "October 27 New Store Products / October 2022";
$string_array = explode(' ', $string, 3);
if (array_key_exists(2, $string_array)) echo $string_array[2];
or Excel:
=RIGHT(A1,LEN(A1)-FIND(" ",A1,FIND(" ",A1)+1))
I've a big dilemma how can I do a condition to remove this type of color from my string (ex: {dd2e22}) using sscanf, which is only func I want to use. So the string provided will be some random text:
Te{dd2e22}xt is {3f53ec}here
The condition what I tried
sscanf(buf,"%[^\{[0-9a-fA-F]{6,8}\}]s",output);
This isn't working, the result are only first character "T".
Try using the format specifier:
"%*6X"
Analysis:
% -- starts a format specifier.
* -- tells scanf not to assign field to variable.
6x -- says that field is 6 hex digits.
See scanf format specifier
result are only first character "T".
Well, the next character is 'e', which matches the set \{[0-9a-fA-F]{6,8}\ and thus doesn't match the inverted set specified by '^'.
This task can be achieved with a regular expression. The standard library provides you with appropriate tools in the <regex> header.
I am processing some HTML and it had some odd characters (which give a line feed when I use the print command) so I did the following:
d.each_char do |c|; puts c + " " + c.ord.to_s; end
I found it was a character with an ord of 9644. It seems this is Unicode black rectangle. There is also a ASCII 219 that looks similar so I wanted to map it to this ASCII code. I tried:
d = d.gsub( 9644.chr, 219.chr)
This gave me an error "Exception: RangeError: 9644 out of char range".
Is there any way I can do this (i.e. change all ord.9644 to ord.219.
Alternatively can I change all characters over ASCII 255 to '?', even if I can it would be good to know how to do this.
Regards,
Ben
How about:
d.chars.map(&:ord).map { |int| int == 9644 ? 219 : int }.map(&:chr).join
If you want to simply replace all high values with ?, then use this instead:
high_limit = 999 # Use whatever integer your `#chr` method can handle
d.chars.map(&:ord).map { |int| int > high_limit ? 255 : int }.map(&:chr).join
The #chars method breaks a string up into an array of single characters, then map(&:ord) converts it into an array of integers representing the UTF-8 codes. The map with the conditional block replaces each occurrence of 9644 with 219 and leaves other integers intact. Finally we map the integers back to single characters with map(&:chr) and then join the resulting array of characters back into a string.
Note that this code creates a few arrays along the way, each of which is equal in size to the length of the original string, so using it on very large strings may consume a fair amount of memory. If this is the case, you might consider replacing map with map! to modify the array in place.
I am trying to write a regex for mysql in PHP to find (at least one occurrence of) exactly 3 of the same characters in a row, but not 4 (or more) of the same.
Eg for "000" I want to find:
0//////0/00/ LS///////000
000////0/00/ LS//////////
0//////0/00/ LS////000///
0//////000// LS//////000/
0//////000// LS//00000000
but not:
0//////0000/ LS//////////
0//////0000/ LS//////////
0/////00000/ LS//////////
I have tried the code below which I thought would match 3 zeros preceded and followed by zero or more chars which are not 0, but this resulted in some rows with single 0's and some 000000's
REGEXP '[^0]*[0{3}][^0]*'
Many thanks.
If you plan to use a regex in MySQL, you cannot use lookarounds. Thus, you can use alternation with negated character class and anchors:
(^|[^0])0{3}([^0]|$)
See the regex demo
Explanation:
(^|[^0]) - a group matching either the start of string (^) or a character other than 0
0{3} - exactly 3 zeros
([^0]|$) - a group matching either a character other than 0 or the end of string ($).
I used SBJsonParser to parse a json string.
inside, instead of hebrew chars, I got a string full of chars in a form like \U05de
what would be the best way to decode these back to hebrew chars,
so i can put these on controls like UIFieldView?
Eventually I ran a loop iterating in the string for the chars \u
in the loop, when detected such a substring, i took a range of 6 characters since that index,
giving me a substring for example \u052v that need to be fixed.
on this string, i ran the method [str JSONValue], which gave me the correct char, then i simply replaced all occurrences of \u052v (for example) with the latter corrected char.