x

Main chapters

  1. LimeSurvey Cloud vs LimeSurvey CE
  2. LimeSurvey Cloud - Quick start guide
  3. LimeSurvey CE - Installation
  4. How to design a good survey (Guide)
  5. Getting started
  6. LimeSurvey configuration
  7. Introduction - Surveys
  8. View survey settings
  9. View survey menu
  10. View survey structure
  11. Introduction - Questions
  12. Introduction - Question Groups
  13. Introduction - Surveys - Management
  14. Survey toolbar options
  15. Multilingual survey
  16. Quick start guide - ExpressionScript
  17. Advanced features
  18. General FAQ
  19. Troubleshooting
  20. Workarounds
  21. License
  22. Version change log
  23. Plugins - Advanced
 Actions

Using regular expressions/ja: Difference between revisions

From LimeSurvey Manual

Bravehorse (talk | contribs)
Created page with "=テキストの検証="
Bravehorse (talk | contribs)
Created page with "現在、自由回答(短い・複数)質問タイプでは、最小値・最大値はサポートしていません。別のやり方として、正規表現つきで自..."
Line 246: Line 246:
=テキストの検証=
=テキストの検証=


currently multiple short text doesn't support minimum or maximum answers. One way around this is to use a long free text type question with a regular expression.
現在、自由回答(短い・複数)質問タイプでは、最小値・最大値はサポートしていません。別のやり方として、正規表現つきで自由回答(長い)質問タイプを使用する方法があります。


The following test for at least one word per line for at least 3 lines and no more than 10 lines.
The following test for at least one word per line for at least 3 lines and no more than 10 lines.

Revision as of 06:16, 16 June 2018

正規表現は始めと終わりがスラッシュ("/")でなければなりません。http://www.regexlib.net/に正規表現の事例集があります。スラッシュで挟まれたものはほぼすべて機能します。

こちらの正規表現テスターで自作の正規表現をテストすることができます。

無事にテストをパスした正規表現を使ってください。

例(すべて1行で記述することに注意してください):

重要: 条件における正規表現

条件エディターで正規表現を使用する場合は、前後のスラッシュを含めないでください。

メールアドレスの検証

/^(\w[-._+\w]*\w@\w[-._\w]*\w\.\w{2,3})$/

郵便番号

オーストラリアの郵便番号

/^[0-9]{4}/

ブラジルの郵便番号

/^[0-9]{2}\.[0-9]{3}-[0-9]{3}$/

カナダの郵便番号

/^[a-zA-Z]\d{1}[a-zA-Z](\-| |)\d{1}[a-zA-Z]\d{1}$/

アメリカの郵便番号

/^[0-9]{5}([- /]?[0-9]{4})?$/

イギリスの郵便番号

/^[A-Z][A-Z]?[0-9][A-Z0-9]? ?[0-9][ABDEFGHJLNPQRSTUWXYZ]{2}$/i

この表現は厳密には正確ではなく、実際の検証はもっと複雑です。詳細は、StackOverflow answerWikipediaなどを参照してください。

電話番号

アメリカの電話番号

/^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/

または

/^[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{3}[\(\)\.\- ]{0,}[0-9]{4}[\(\)\.\- ]{0,}$/

二つ目の例は、数字以外の記号を含むカナダとアメリカの電話番号用です。

 . ( ) - (space)

この表現では、次のような電話番号にもマッチします。

  • (555)555 5555
  • 555.555.5555
  • 555 555 5555
  • (555)-555-5555
  • 555-555-5555
  • 555555555

オーストラリアの電話番号

次のパターンは、"+61"の国コードを含む、オーストラリアのすべての携帯・固定の電話番号にマッチします。

  • (02) 9123 6535
  • 03 1234-5345
  • 0412 345 678
  • +61 2 3456 789

以下のものにはマッチしません。

  • 234 3450 234
  • a234 534 3432
  • 134567
  • 123456789013

括弧、スペース、ハイフンは無視されます。

次に挙げる'正確版'は、最初の4ないし5桁が有効なオーストラリアの電話番号であることを確認します。

'簡易版'では、最初の2桁しか見ていないため、無効な番号にもマッチします。

すべてのオーストラリアの電話番号(携帯・固定、市外局番込み)

正確版:

/^\(?(?:\+?61|0)(?:(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}|4\)?[ -]?(?:(?:[01][ -]?[0-9]|2[ -]?[0-57-9]|3[ -]?[1-9]|4[ -]?[7-9]|5[ -]?[018])[ -]?[0-9]|3[ -]?0[ -]?[0-5])(?:[ -]?[0-9]){5})$/


簡易版:

/^(?:\+?61|0)[2-478](?:[ -]?[0-9]){8}$/

すべてのオーストラリアの電話番号(固定のみ、市外局番含む)

正確版:

/^\(?(?:\+?61|0)(?:2\)?[ -]?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])|3\)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])|7\)?[ -]?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)|8\)?[ -]?(?:5[ -]?[1-4]|6[ -]?[0-8]|[7-9][ -]?[0-9]))(?:[ -]?[0-9]){6}$/


簡易版:

/^(?:\+?61|\(?0)[2378]\)?(?:[ -]?[0-9]){8}$/

ニューサウスウェールズの固定電話番号(市外局番は省略可)

正確版:

/^(?:\(?(?:\+?61|0)2\)?[ -]?)?(?:3[ -]?[38]|[46-9][ -]?[0-9]|5[ -]?[0-35-9])(?:[ -]?[0-9]){6}$/

簡易版:

/^(?:\(?(?:\+?61|0)2\)?(?:[ -]?[0-9]){7}[0-9]$/

ビクトリア、タスマニアの固定電話番号(市外局番省略可)

正確版:

/^(?:\(?(?:\+?61|0)3\)?[ -]?)?(?:4[ -]?[0-57-9]|[57-9][ -]?[0-9]|6[ -]?[1-67])(?:[ -]?[0-9]){6}$/

簡易版:

/^(?:\(?(?:\+?61|0)3\)?(?:[ -]?[0-9]){7}[0-9]$/

クイーンズランドの固定電話番号(市外局番は省略可)

正確版:

/^(?:\(?(?:\+?61|0)7\)?[ -]?)?(?:[2-4][ -]?[0-9]|5[ -]?[2-7]|7[ -]?6)(?:[ -]?[0-9]){6}$/


簡易版:

/^(?:\(?(?:\+?61|0)7\)?(?:[ -]?[0-9]){7}[0-9]$/

南オーストラリア、北部準州、西オーストラリアの固定電話番号(市外局番は省略可)

正確版:

/^(?:\(?(?:\+?61|0)8\)?[ -]?)?(?:5[1-4]|6[0-8]|[7-9][0-9])$/

簡易版:

/^(?:\(?(?:\+?61|0)8\)?(?:[ -]?[0-9]){7}[0-9]$/

オーストラリアの携帯電話番号

正確版:

/^(?:\+?61|0)4 ?(?:(?:[01] ?[0-9]|2 ?[0-57-9]|3 ?[1-9]|4 ?[7-9]|5 ?[018]) ?[0-9]|3 ?0 ?[0-5])(?: ?[0-9]){5}$/


簡易版:

/^(?:\(?(?:\+?61|0)4\)?(?:[ -]?[0-9]){7}[0-9]$/

ベルギーの電話番号

/^((\+|00)32\s?|0)(\d\s?\d{3}|\d{2}\s?\d{2})(\s?\d{2}){2}$/

ベルギーの携帯電話番号

/^((\+|00)32\s?|0)4(60|[789]\d)(\s?\d{2}){3}$/

フランスの電話番号

/^((\+|00)33\s?|0)[1-5](\s?\d{2}){4}$/

フランスの携帯電話番号

/^((\+|00)33\s?|0)[679](\s?\d{2}){4}$/

ルクセンブルクの電話番号

/^((\+|00\s?)352)?(\s?\d{2}){3,4}$/

ルクセンブルクの携帯電話番号

/^((\+|00\s?)352)?\s?6[269]1(\s?\d{3}){2}$/

ドイツマルク(正負記号付きも可)

/^[1-6]{1}[\+|\-]?$/

年齢の検証

例: 20-99歳

/([2-9][0-9])/


例: 18-35歳

/(1[8-9]|2[0-9]|3[0-5])/


例: 19-65歳

/^(1[8-9]|[2-5][0-9]|6[0-5])$/

数値の検証

1から99999までの数値

/^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9]|[1-9][0-9][0-9][0-9][0-9])$/
/^[1-9][0-9]{0,4}$/ も同じですが、少し早く動作します

1から999、1.000から999.999、999.999.999まで

/^[1-9][0-9]{0,2}(?:\.[0-9]{3}){0,2}$/

1から999、1.000から999.999、999.999.999までにマッチしますが、999.1.1、94.22.22、999.1.22、999.11.1、999.1.333といったものにはマッチしません。

小数点込みの数値の検証(価格向け)

0から199(小数点以下2桁があっても可)にマッチさせる例。

/^([1][0-9][0-9]|[1-9][0-9]|[0-9])((\.)[0-9][0-9])?$/


小数点以下2桁が必須で、3桁ずつのカンマ区切り(省略可)があり、1.00から999,999,999.00までにマッチさせる例。1.00、1,000.00、12,345.67、12345,02、123,456,468.00、1234566.00、123456789.00にマッチし、1,23.00、12,3.4、1234,43.04にはマッチしない。

/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/

上記に似た例: 小数点以下2桁が必須で、桁区切り","の前に"0"を許容する例。

/[0-9]{0,2}(?:,?[0-9]{3}){0,3}\.[0-9]{2}$/

上記と同様で、小数点以下を省略可とする例:

/^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(?:\.[0-9]{2})?$/

月(1-12)

誕生した月に関する質問の回答を検証する場合:

/^[0]*[1-9]$|^[0]*1[0-2]$/

最小の桁数(下記の例では3桁以上)

/^.{3,}$/

通貨

アメリカの通貨($記号、セント)

/^\$?\d+(\.(\d{2}))?$/

カンマを使用する場合:

/^\$?\d{1,3}(\d+(?!,))?(,\d{3})*(\.\d{2})?$/

スイスの価格表示

小数点以下に2桁あり、小数点以下第2位の桁が0か5となる数値:

/^(\d+)(\.\d(05)?)?$/

点数の検証

1から10

/^[1-9]{1}$|^10$/

1から100

/^[1-9]?[0-9]{1}$|^100$/

テキストの検証

現在、自由回答(短い・複数)質問タイプでは、最小値・最大値はサポートしていません。別のやり方として、正規表現つきで自由回答(長い)質問タイプを使用する方法があります。

The following test for at least one word per line for at least 3 lines and no more than 10 lines.

/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){2,10}/is

If you wanted, say five words per line you could change the first and last star/asterisk to {4,} e.g.

/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+){4,})){2,10}/is

If you wanted one or more words per line on between 1 and 5 lines, you can change the content of the last curley braces to 0,4 (note you use 0 because you're already matching the first line).

/(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)(?:[,.;:?!& \n\r]?(?:\n|\r|\n\r|\r\n)(?:[^,.;:?!& \n\r]+(?: [^,.;:?!& \n\r]+)*)){0,4}/is

Word count

The following restricts the number of words allowed to a minimum of 1 and a maximum of 200
/^[-\w]+(?:\W+[-\w]+){0,199}\W*$/
To increase the minimum change the zero part of {0,199}

To increase or decrease the maximum change the "199" part of {0,199}

Time validation

There are a number of ways of writing time formats. Some of the possible options are 12 hour or 24 hour, with seconds or without. Although it is an option to use the date question type (it can also capture time) you can use "short free text" with one of the validation regular expressions below:

The following three validation strings test for 24 hour time (in order of appearences) without seconds, with optional seconds lastly with seconds required.

/^(?:[01][0-9]|2[0-3]):[0-5][0-9]$/
/^(?:[01][0-9]|2[0-3]):[0-5][0-9](?::[0-5][0-9])?$/
/^(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/


The following three match 12 hour time, as above with seconds, optional seconds and with seconds required

/^(?">00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))$/
/^(?:00:[0-5][0-9](?::[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?::[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?::[0-5][0-9])? (?:pm|PM))$/
/^(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))$/


The following three match either 12 or 24 hour time as above with seconds, optional seconds and with seconds required

/^(?:(?:00:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9])$/
/^(?:(?:00:[0-5][0-9](?[0-5][0-9])? (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9](?[0-5][0-9])? (?:[ap]m|[AP]M)|12:[0-5][0-9](?[0-5][0-9])? (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9](?[0-5][0-9])?)$/
/^(?:(?:00:[0-5][0-9]:[0-5][0-9] (?:am|AM)|(?:0[1-9]|1[01]):[0-5][0-9]:[0-5][0-9] (?:[ap]m|[AP]M)|12:[0-5][0-9]:[0-5][0-9] (?:pm|PM))|(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9])$/

US states

To validate for one state use the following (example is Texas):

  • TX uppercase only =
    /^(TX)$/
    
  • tx lowercase only =
    /^(tx)$/
    
  • TX upper or lowercase =
    /^([T|t][X|x])$/
    

Profanity Filter

To filter profanity words from an answer:

/^(?i)((?!\bENTERPROFANITYHERE\b).)*$(?-i)/

Replace "ENTERPROFANITYHERE" with your bad word.

The \b will allow passing of words such as "assassination" & "hello" if you enter "ass" or "hell" as your profanity word. This also works if you are trying to omit other words, names etc. from answers.