🖥

Regular Expression | Do you know the x option? (Easy to read even a li

2019/04/16に公開

The x option will ignore spaces.

problem

For example, what is the following regular expression doing? Can you imagine at a glance?

 /\A(\d{3}-\d{3}-\d{1,3})|(\d{3}-\d{1,3})|(\d{1,9})\z/ 

I do not understand.

Solution

If you specify the x option as follows: Ignore space And regular expressions that are difficult to understand in the first place. You can make it a little easier to read.

 /\A ( \d{3}-\d{3}-\d{1,3}) | (\d{3}-\d{1,3}) | (\d{1,9} ) \z/x 

More readable (break line)

It is also possible to break the line if it is a permissible programming language.

 / \A (\d{3}-\d{3}-\d{1,3}) | (\d{3}-\d{1,3}) | (\d{1,9}) \z /x 

What's wrong?

  • Is not it a problem of getting used to?
  • In the case of regular expressions, it is common to "pack and write" for some reason. But imagine, for example, "write programming code without line breaks" or "write SQL without line breaks".

Original by

正規表現 | x オプションをご存知ですか? ( 少しでも読みやすく )

About

About this translattion

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-04-16

Discussion