About regular expressions used in ruby sub method

Regular expression doesn't work

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

I have an example of the answer, so below

address.sub(/[^,]+,/,'')

Briefly explain [^,] +,

[^,] 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

About regular expressions used in ruby sub method
About regular expressions in Ruby
About Ruby regular expressions (metacharacters, captures, related methods)
About eval in Ruby
About method matchers used in model unit test code (RSpec)
Handling of line beginning and line ending in regular expressions in Ruby
Rails: Capture regular expressions in emails!
[Super Introduction] About Symbols in Ruby
[Ruby] undefined method `dark?'occurs in rqr_code
Ruby methods often used in Rails
Regular expressions
Implemented "Floyd Cycle Detection Method" in Ruby
About the phenomenon that StackOverflowError occurs in processing using Java regular expressions
About the case that ("b" .. "aa") could not be used in Ruby Range
[Ruby] How to use gsub method and sub method
About var used in Java (Local Variable Type)
Match IP addresses using regular expressions in Java
[Ruby] Assign variables in conditional expressions in if statements.
About characters that are completed in method arguments
Ruby to_s method
About Ruby symbols
About Ruby Hashes
Class in Ruby
[Ruby] slice method
[Ruby] end_with? method
[Ruby] Method memorandum
About the method
About Ruby arrays
About Ruby inheritance
About ruby block
About Ruby Hashes
Heavy in Ruby! ??
About Ruby Symbols
Ruby build method
About Ruby variables
Ruby regular expression
About Ruby methods
Read mp3 tag (ID3v1) in Ruby (no library used)
[Ruby] Exclude and replace specific patterns with regular expressions
Examine the elements in the array using the [Ruby] includes? Method
About the difference between classes and instances in Ruby
[For beginners] ○○. △△ in Ruby (ActiveRecord method, instance method, data acquisition)
Be careful about Ruby method calls and variable references
Notes on how to use regular expressions in Java