Unable to Match numbers only using Regular Expression in golang using Regex - go

matched, err := regexp.MatchString(`[0-9]`, `a.31`)
fmt.Println(matched)
The above expression is returning true.. Isn't it supposed to be false?
I want to extract only numbers but why is "a.31" considered true? I've noticed that having atleast a number in the string will return "true"...
How to make it return "true" only for numbers?

Related

Golang Typecasting

I have specific questions for my project
input = "3d6"
I want to convert this string some parts to integer. For instance I want to use input[0] like integer.
How can I do this?
There's two problems here:
How to convert a string to an integer
The most straightforward method is the Atoi (ASCII to integer) function in the strconv package., which will take a string of numeric characters and coerce them into an integer for you.
How to extract meaningful components of a known string pattern
In order to use strconv.Atoi, we need the numeric characters of the input by themselves. There's lots of ways to slice and dice a string.
You can just grab the first and last characters directly - input[:1] and input[2:] are the ticket.
You could split the string into two strings on the character "d". Look at the split method, a member of the strings package.
For more complex problems in this space, regular expressions are used. They're a way to define a pattern the computer can look for. For example, the regular expression ^x(\d+)$ will match on any string that starts with the character x and is followed by one or more numeric characters. It will provide direct access to the numeric characters it found by themselves.
Go has first class support for regular expressions via its regexp package.
For example,
package main
import (
"fmt"
)
func main() {
input := "3d6"
i := int(input[0] - '0')
fmt.Println(i)
}
Playground: https://play.golang.org/p/061miKcXdIF
Output:
3

regexp find for Chinese unicode character [duplicate]

This question already has answers here:
Why does "Year 2010" =~ /([0-4]*)/ results in empty string in $1?
(7 answers)
Closed 6 years ago.
i have code like this
re, err = regexp.Compile(`\p{Han}*`)
if err != nil {
fmt.Println(err)
return
}
s := "foo中文哦woqu"
fmt.Println(re.FindString(s))
but it print empty.
and then i change \p{Han}* to \p{Han}+, it display's 中文哦.
change \p{Han}* to \p{Han}?, it print empty.
I find the document like this:
x* zero or more x, prefer more
x+ one or more x, prefer more
x? zero or one x, prefer one
so i expect my print is:
\p{Han}* print 中文哦
\p{Han}+ print 中文哦
\p{Han}? print 中
could someone tell me what happened?
As the docs say (emphasis added):
FindString returns a string holding the text of the leftmost match in s of the regular expression. If there is no match, the return value is an empty string, but it will also be empty if the regular expression successfully matches an empty string. Use FindStringIndex or FindStringSubmatch if it is necessary to distinguish these cases.
\p{Han}* matches an empty string. You can also see that by using FindAllString:
fmt.Printf("%q", re.FindAllString(s, -1))
// Prints ["" "" "" "中文哦" "" "" "" ""]
You can use \p{Han}+ which doesn't match an empty string.

String matching regex syntax issue

I am trying to write a method to pulls out every string that matches the criteria from a large text file:
Every element is separated by a comma
The first 5 elements could be any number from 1-59
The next 21 elements should be numbers from 1-5
The next 27 elements could be either true or false (no caps)
The final 5 elements are integers from 1-5
My code:
#this string should be returned by the regex matching
str="3,15,14,31,40,5,5,4,5,3,4,4,5,2,2,2,1,2,1,1,3,3,3,2,4,3,false,false,false,false,false,true,false,true,false,false,false,false,false,false,false,true,false,false,false,false,false,false,false,false,false,false,false,3,3,3,2,3"
matchResult=/[1-59]{5}[1-5]{21}[true|false]{27}[1-5]{5}/.match(str)
matchResult.each{|x| #this doesnt work....why?
puts x
}
What is the correct way to print all matches? matchResult.each throws an error. I thought it returned an array of matches.
How can I adjust my regex to expect a comma between every value (not at the ends of the string)?
Put true or false inside a capturing group or non-capturing group like (?:true|false) so that it would match the exact true or false substring , this [true|false] will match a single character only whether it may be t or r or u or e or | ,....
> str="3,15,14,31,40,5,5,4,5,3,4,4,5,2,2,2,1,2,1,1,3,3,3,2,4,3,false,false,false,false,false,true,false,true,false,false,false,false,false,false,false,true,false,false,false,false,false,false,false,false,false,false,false,3,3,3,2,3"
> str.match(/^(?:[1-5]\d|[1-9])(?:,(?:[1-5]\d|[1-9])){4}(?:,[1-5]){21}(?:,(?:true|false)){27}(?:,[1-5]){5}$/)
=> #<MatchData "3,15,14,31,40,5,5,4,5,3,4,4,5,2,2,2,1,2,1,1,3,3,3,2,4,3,false,false,false,false,false,true,false,true,false,false,false,false,false,false,false,true,false,false,false,false,false,false,false,false,false,false,false,3,3,3,2,3">
In regards to your first question:
"What is the correct way to print all matches? matchResult.each throws an error. I thought it returned an array of matches."
The Regex .match method doesn't return an array of matches; it returns either a match object (in this case, a string, since you're calling .match on a string) or nil if there are no matches (see docs here).
This means matchResult is a string, and you can't call .each on a string, which is why you're getting an error message. See this post for more info on .each & strings.

NULL literal in XPath

Is there a NULL literal in XPath 1.0 or 2.0?
My use case is that I have a conditional (if then else) XPath expression and I want to return NULL to signify a certain condition. I am afraid that returning an empty string might be ambiguous in my case as it could be a valid result of the other part of the if then else expression.
The empty sequence () can be used as such. It is also returned if there is no result for a path expression.
let $foo := "foo"
return
if ($foo = ("foo", "bar", "batz")) then
$foo
else
()
You can check for an empty sequence using
let $result := ()
return empty($result)
If you pass the result of the first XPath expression to your native code, you should be able to distinguish "NULL" from the empty string by having no results (empty sequence / "NULL") or having a result string (which could be empty).

How can I get Browser.text.include? to be case insensitive?

It's as simple as that:
How can I get Browser.text.include?, or Ruby in general, to be case insensitive for that specified command?
One of the easiest ways is to downcase or upcase the text that you're reading:
Browser.text.downcase.include?
Then, you need to make sure that your desired text is supplied in all lowercase.
You can use String#match with a regular expression.
e.g.:
("CaseSensitive".match /SENSITIVE/i) != nil
That will return true if there is a case-insensitive match, false otherwise.
So for the above example, it returns true, as 'SENSITIVE' is found within 'CaseSensitive'.
For your example:
(Browser.text.match /yourString/i ) != nil

Resources