address = "prefecture, city, street address, building name room number" There is a variable called, and you want to get the street address from here. In conclusion
address.split(",")[2]
but, I challenged with regular expressions.
First of all, in sub, I deleted the first "prefecture," and decided to repeat it twice. From the remaining "address, building name, room number", delete the "," and after to finish.
First test with a one-time version
address.sub(/.+,/,'')
Only "prefectures," will disappear.
result "Building name, room number" It has become.
I predicted that the /. +, / Part would be "prefecture," It becomes "prefecture, city, street address,".
As a test
address.sub(/,/,'')
If you try "Prefectures, cities, wards, towns and villages, street numbers, building names, room numbers" And it will get rid of the first one properly.
Why? ?? ??
・ ・ ・ ・ ・ ・ ・ Well, the problem of getting only the street address has been solved. I wonder if that is the case now, and I will move on to the next. I'm still studying ... If I study in the future, this question may clear up ... If you like, please tell me TT
address.sub(/[^,]+,/,'')
[^,] Will be characters other than ,. As an example, [^ A] is a character other than A. There is at least one character other than [^,] +. There is one or more characters other than A with [^ A] +. [^,] +, After one or more characters other than, are followed by ,. [^ A] +, after one or more characters other than A continue.
The first. +, Was, after any character followed, so it was the prefecture, city, street address, and so on. , Was also regarded as an arbitrary character, so I think this was the result. Therefore, I think that it became a prefecture by using characters other than ,.
This is very easy to understand, so I will introduce it ^ ^ [Beginners welcome! Introduction to regular expressions that can be learned by hand and eyes ・ Part 1 "Let's search for phone numbers in various formats"](https://qiita.com/jnchito/items/893c887fbf19e17d3ff9 "Beginners welcome! Introduction to regular expressions that can be learned by hand and eyes" Part 1 "Let's search for phone numbers in various formats" ") [Beginners welcome! Introduction to regular expressions that can be learned by hand and eyes, part 2 "Let's replace while allowing subtle differences"](https://qiita.com/jnchito/items/64c3fdc53766ac6f2008 "Beginners welcome! Introduction to regular expressions that can be learned by hands and eyes" Part 2 "Let's replace while allowing subtle differences" ")
Thank you for introducing the article ^ ^
Recommended Posts