官术网_书友最值得收藏!

Using regex as an IF statement

In this recipe, we will write a simple program that will function as an if statement. A simple find regex statement will be used.

Getting ready

We will first write an if statement that will check if the value of a parameter variable field1 has the value equal to ABC, DEF, or CDE. In case the value is equal to any of the three, the message Field Value is Valid is displayed. We will then see the equivalent regex.

Getting ready

How to do it...

For replacing the if statement with find regex statement, proceed as follows:

  1. Instead of the if statement, we will write a find regex statement along with the regex '[ABC|CDE|DEF]'.
  2. After the statement, the sy-subrc is checked, and the appropriate messages are written.
    How to do it...

How it works...

We have used an OR (|) operator within the find statement. A match is found if the value of the three-character field1 is equal to any of the three values specified. In this case, sy-subrc is equal to zero, and the success message is then displayed.

There's more...

Suppose we need to ignore the case. Say the input is to be compared such that there is no difference between ABC, abc, and ABc. In such a case, we may simply add ignoring case to the statement, as shown in the following screenshot:

There's more...
主站蜘蛛池模板: 南雄市| 调兵山市| 迁西县| 子洲县| 彩票| 西平县| 乌鲁木齐县| 平和县| 十堰市| 睢宁县| 田林县| 灵寿县| 施秉县| 玉屏| 丁青县| 徐闻县| 徐州市| 白城市| 尤溪县| 华蓥市| 韩城市| 朝阳市| 慈溪市| 曲靖市| 抚顺市| 贞丰县| 固安县| 涟水县| 松滋市| 石河子市| 岱山县| 永嘉县| 蒙城县| 安吉县| 鄂托克前旗| 阜阳市| 苍溪县| 牟定县| 普兰店市| 瓦房店市| 昌邑市|