Why does this regular expression match? [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
"Galaxy".match(/(al)*/)
It seems that <<>>Galaxy is the match, where <<>> = matching part. Why is the R.E working? Perhaps because of \b?
RE:
Sorry, it's my fault to post unclear question.
Exactly, i want to know the reason that why the empty space is matched with (al)*.
Finally i could understand by you-all favor :)

The regex /(al)*/ allows it to match nothing at all, which is what it does. It starts at the beginning of the string, matches "nothing" and returns. If you expected it to match the al in Galaxy then you would need to use /(al)+/ to avoid empty matches.

Related

How to write a file in ruby without a terminating newline [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
For debugging purposes, I want to alter a rake test so as not to end a saved file with a newline.
I don't know ruby. How do I do this? (file.print doesn't seem to work.)
Not clear what you are doing, but since you tried file.print, it looks like you have access to what is to be printed. Let's say this is string. My guess is that string already has a newline character. Then do:
file.print(string.chomp)

ruby regex with address conversion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
東京都building千代田区丸の内1building2floor1number
to
東京都千代田区丸の内1-2-1
PS: Digits are double bytes.I have a string,that contains address. How to convert that address like above.
Answering the question that probably should have been asked (given the clarifications in comments):
address.gsub(/([0-9]+)(?:丁目|番地?|号)(?=[0-9]|$)/, '\1-').sub(/-$/, '')
This will process any sequences of "number+location" (with "location" being lot, block, (house) number...) that is either followed by another number, or is at the end of the string, replacing the location with a dash; then removing the final dash, if there is one.
Note that this will not pick up on Japanese numbers: if you get an address like 東京都千代田区丸の内一丁目2番1号, it makes no sense to transform it to 東京都千代田区丸の内一-2-1, and converting Japanese numbers into Arabic ones is trivial up to 9 and then a bit less trivial.
string.gsub(/(?<=[0-9])(?:丁目|番地?|号)\b/, "-").sub(/\z/, "")

How to split the first and the last element on a Ruby string? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to split a string in 2 parts: the first word and the last one.
I know I need to use the .split method but don't know how.
Is this what you want to do?
first, *_, last = "now is the time for all".split
first #=> "now"
last #=> "all"

Parsing using scan and regex [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to use scan in particular to get the data and the data only between two values using scan and a regex expression.
The values aren't static so I can't match using them.
Let me rephrase it, i need the value only. So something that allows me to get everything after id="results" value=" and before "
id="results" value="/randompath/lol.jpg"
I got no clue what you're actually searching for but if you want to extract the value of the attribute value in that string (assuming double quotes).
You can use this regex:
(?:^|\s|")value(?:\s+)?=(?:\s+)?(?:"((?:\\.|[^"])+)")
But for sure any html/xml parser like Nokogiri would be better if you can use one.

ruby regex for validating and parsing "1,a|2,b|3,c" string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am in a search of one regular expression which validates the following string format "1,a|2,b|3,c".
Also I want to parse this string and extract out the numbers and characters.
Can any one has any idea on what could be the best regular expression.
Thanks
Try this expression: http://regex101.com/r/qI7kW9
/(\d),([a-z])(?:\||$)/gi
The first capturing group will hold the digit, the second will hold the letter. If there will be more than one character that you want to capture, use this:
/(\d+),([a-z]+)(?:\||$)/gi

Resources