Regex: Find strings without character












0















i build regex expression that matches
2 letters or 2 letters folowed by '/' and next 2 letters for example:



rt bl/ws se gn/wd wk bl/rt

/^(((s+)?[a-zA-Z]{2}(/[a-zA-Z]{2})?)(s+|$))+$/i


and that works without problems.
Next problem what I have is match all "word" not containing '/' character.
and replace all matches by duplicate values separated by '/'. For above example excepted output should be:



rt/rt bl/ws se/se gn/wd wk/wk bl/rt


I tried it some time but without success. Could you help me with that ?



Update 1:



I've started with regex that matches words not containing 'at'



(b((?!(at))w)+b)


Et the and I want to replace matched elements with python like



re.sub(r'(b((?!(at))w)+b)', r'1/1', text)


but first have to find right elements ...










share|improve this question




















  • 1





    Show us what you tried, it will help identify your problem.

    – Wiktor Stribiżew
    Nov 20 '18 at 22:11











  • Also, it would be great if you mentioned the programming language you are using the regex in.

    – Wiktor Stribiżew
    Nov 20 '18 at 22:15











  • Your regex is very complicated. You could simplify it. For instance, (s+)? is the same as s*. Also have a look at w and b.

    – Socowi
    Nov 20 '18 at 22:16











  • @Wiktor Stribiżew OP never excepts that work will be made by someone else. Is there ask for help not right place on this forum ?

    – walko1234
    Nov 20 '18 at 22:53











  • Then you may use re.sub(r'(?<!S)[^Wd_]+(?!S)', r'g<0>/g<0>', s). Or re.sub(r'(?<!S)([^Wd_]+)(?!S)', r'1/1', s). To exclude at word matches, use re.sub(r'(?<!S)(?!at(?!S))([^Wd_]+)(?!S)', r'1/1', s)

    – Wiktor Stribiżew
    Nov 21 '18 at 0:40


















0















i build regex expression that matches
2 letters or 2 letters folowed by '/' and next 2 letters for example:



rt bl/ws se gn/wd wk bl/rt

/^(((s+)?[a-zA-Z]{2}(/[a-zA-Z]{2})?)(s+|$))+$/i


and that works without problems.
Next problem what I have is match all "word" not containing '/' character.
and replace all matches by duplicate values separated by '/'. For above example excepted output should be:



rt/rt bl/ws se/se gn/wd wk/wk bl/rt


I tried it some time but without success. Could you help me with that ?



Update 1:



I've started with regex that matches words not containing 'at'



(b((?!(at))w)+b)


Et the and I want to replace matched elements with python like



re.sub(r'(b((?!(at))w)+b)', r'1/1', text)


but first have to find right elements ...










share|improve this question




















  • 1





    Show us what you tried, it will help identify your problem.

    – Wiktor Stribiżew
    Nov 20 '18 at 22:11











  • Also, it would be great if you mentioned the programming language you are using the regex in.

    – Wiktor Stribiżew
    Nov 20 '18 at 22:15











  • Your regex is very complicated. You could simplify it. For instance, (s+)? is the same as s*. Also have a look at w and b.

    – Socowi
    Nov 20 '18 at 22:16











  • @Wiktor Stribiżew OP never excepts that work will be made by someone else. Is there ask for help not right place on this forum ?

    – walko1234
    Nov 20 '18 at 22:53











  • Then you may use re.sub(r'(?<!S)[^Wd_]+(?!S)', r'g<0>/g<0>', s). Or re.sub(r'(?<!S)([^Wd_]+)(?!S)', r'1/1', s). To exclude at word matches, use re.sub(r'(?<!S)(?!at(?!S))([^Wd_]+)(?!S)', r'1/1', s)

    – Wiktor Stribiżew
    Nov 21 '18 at 0:40
















0












0








0


0






i build regex expression that matches
2 letters or 2 letters folowed by '/' and next 2 letters for example:



rt bl/ws se gn/wd wk bl/rt

/^(((s+)?[a-zA-Z]{2}(/[a-zA-Z]{2})?)(s+|$))+$/i


and that works without problems.
Next problem what I have is match all "word" not containing '/' character.
and replace all matches by duplicate values separated by '/'. For above example excepted output should be:



rt/rt bl/ws se/se gn/wd wk/wk bl/rt


I tried it some time but without success. Could you help me with that ?



Update 1:



I've started with regex that matches words not containing 'at'



(b((?!(at))w)+b)


Et the and I want to replace matched elements with python like



re.sub(r'(b((?!(at))w)+b)', r'1/1', text)


but first have to find right elements ...










share|improve this question
















i build regex expression that matches
2 letters or 2 letters folowed by '/' and next 2 letters for example:



rt bl/ws se gn/wd wk bl/rt

/^(((s+)?[a-zA-Z]{2}(/[a-zA-Z]{2})?)(s+|$))+$/i


and that works without problems.
Next problem what I have is match all "word" not containing '/' character.
and replace all matches by duplicate values separated by '/'. For above example excepted output should be:



rt/rt bl/ws se/se gn/wd wk/wk bl/rt


I tried it some time but without success. Could you help me with that ?



Update 1:



I've started with regex that matches words not containing 'at'



(b((?!(at))w)+b)


Et the and I want to replace matched elements with python like



re.sub(r'(b((?!(at))w)+b)', r'1/1', text)


but first have to find right elements ...







javascript regex regex-negation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 7:57









JohnyL

3,7231924




3,7231924










asked Nov 20 '18 at 22:09









walko1234walko1234

466




466








  • 1





    Show us what you tried, it will help identify your problem.

    – Wiktor Stribiżew
    Nov 20 '18 at 22:11











  • Also, it would be great if you mentioned the programming language you are using the regex in.

    – Wiktor Stribiżew
    Nov 20 '18 at 22:15











  • Your regex is very complicated. You could simplify it. For instance, (s+)? is the same as s*. Also have a look at w and b.

    – Socowi
    Nov 20 '18 at 22:16











  • @Wiktor Stribiżew OP never excepts that work will be made by someone else. Is there ask for help not right place on this forum ?

    – walko1234
    Nov 20 '18 at 22:53











  • Then you may use re.sub(r'(?<!S)[^Wd_]+(?!S)', r'g<0>/g<0>', s). Or re.sub(r'(?<!S)([^Wd_]+)(?!S)', r'1/1', s). To exclude at word matches, use re.sub(r'(?<!S)(?!at(?!S))([^Wd_]+)(?!S)', r'1/1', s)

    – Wiktor Stribiżew
    Nov 21 '18 at 0:40
















  • 1





    Show us what you tried, it will help identify your problem.

    – Wiktor Stribiżew
    Nov 20 '18 at 22:11











  • Also, it would be great if you mentioned the programming language you are using the regex in.

    – Wiktor Stribiżew
    Nov 20 '18 at 22:15











  • Your regex is very complicated. You could simplify it. For instance, (s+)? is the same as s*. Also have a look at w and b.

    – Socowi
    Nov 20 '18 at 22:16











  • @Wiktor Stribiżew OP never excepts that work will be made by someone else. Is there ask for help not right place on this forum ?

    – walko1234
    Nov 20 '18 at 22:53











  • Then you may use re.sub(r'(?<!S)[^Wd_]+(?!S)', r'g<0>/g<0>', s). Or re.sub(r'(?<!S)([^Wd_]+)(?!S)', r'1/1', s). To exclude at word matches, use re.sub(r'(?<!S)(?!at(?!S))([^Wd_]+)(?!S)', r'1/1', s)

    – Wiktor Stribiżew
    Nov 21 '18 at 0:40










1




1





Show us what you tried, it will help identify your problem.

– Wiktor Stribiżew
Nov 20 '18 at 22:11





Show us what you tried, it will help identify your problem.

– Wiktor Stribiżew
Nov 20 '18 at 22:11













Also, it would be great if you mentioned the programming language you are using the regex in.

– Wiktor Stribiżew
Nov 20 '18 at 22:15





Also, it would be great if you mentioned the programming language you are using the regex in.

– Wiktor Stribiżew
Nov 20 '18 at 22:15













Your regex is very complicated. You could simplify it. For instance, (s+)? is the same as s*. Also have a look at w and b.

– Socowi
Nov 20 '18 at 22:16





Your regex is very complicated. You could simplify it. For instance, (s+)? is the same as s*. Also have a look at w and b.

– Socowi
Nov 20 '18 at 22:16













@Wiktor Stribiżew OP never excepts that work will be made by someone else. Is there ask for help not right place on this forum ?

– walko1234
Nov 20 '18 at 22:53





@Wiktor Stribiżew OP never excepts that work will be made by someone else. Is there ask for help not right place on this forum ?

– walko1234
Nov 20 '18 at 22:53













Then you may use re.sub(r'(?<!S)[^Wd_]+(?!S)', r'g<0>/g<0>', s). Or re.sub(r'(?<!S)([^Wd_]+)(?!S)', r'1/1', s). To exclude at word matches, use re.sub(r'(?<!S)(?!at(?!S))([^Wd_]+)(?!S)', r'1/1', s)

– Wiktor Stribiżew
Nov 21 '18 at 0:40







Then you may use re.sub(r'(?<!S)[^Wd_]+(?!S)', r'g<0>/g<0>', s). Or re.sub(r'(?<!S)([^Wd_]+)(?!S)', r'1/1', s). To exclude at word matches, use re.sub(r'(?<!S)(?!at(?!S))([^Wd_]+)(?!S)', r'1/1', s)

– Wiktor Stribiżew
Nov 21 '18 at 0:40














2 Answers
2






active

oldest

votes


















0














in python you can do something like:



re.sub(r'(?:(?<=^)|(?<= ))(w+)(?= )',r'1/1','rt bl/ws se gn/wd wk bl/rt')

'rt/rt bl/ws se/se gn/wd wk/wk bl/rt'





share|improve this answer































    0














    If you're using a flavour of regex that supports negative lookbehind this regex should find the strings you want to replace:



    (?<!/)([A-Za-z]{2}(?!/))



    (?<!/) - makes sure there isn't a / behind the match
    ([A-Za-z]{2} looks for two alphabetic characters
    (?!/)) makes sure there isn't a trailing /



    In Python you would use it like this:



    print(re.sub(r'(?<!/)([A-Za-z]{2}(?!/))',r'1/1','rt bl/ws se gn/wd wk bl/rt'))


    Output:



    rt/rt bl/ws se/se gn/wd wk/wk bl/rt


    Demo on rextester



    In PHP you would use it like so:



    $str = 'rt bl/ws se gn/wd wk bl/rt';
    echo preg_replace('/(?<!/)([A-Za-z]{2}(?!/))/', '$1/$1', $str);


    Output:



    rt/rt bl/ws se/se gn/wd wk/wk bl/rt


    Demo on 3v4l.org






    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402349%2fregex-find-strings-without-character%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      in python you can do something like:



      re.sub(r'(?:(?<=^)|(?<= ))(w+)(?= )',r'1/1','rt bl/ws se gn/wd wk bl/rt')

      'rt/rt bl/ws se/se gn/wd wk/wk bl/rt'





      share|improve this answer




























        0














        in python you can do something like:



        re.sub(r'(?:(?<=^)|(?<= ))(w+)(?= )',r'1/1','rt bl/ws se gn/wd wk bl/rt')

        'rt/rt bl/ws se/se gn/wd wk/wk bl/rt'





        share|improve this answer


























          0












          0








          0







          in python you can do something like:



          re.sub(r'(?:(?<=^)|(?<= ))(w+)(?= )',r'1/1','rt bl/ws se gn/wd wk bl/rt')

          'rt/rt bl/ws se/se gn/wd wk/wk bl/rt'





          share|improve this answer













          in python you can do something like:



          re.sub(r'(?:(?<=^)|(?<= ))(w+)(?= )',r'1/1','rt bl/ws se gn/wd wk bl/rt')

          'rt/rt bl/ws se/se gn/wd wk/wk bl/rt'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 23:17









          OnyambuOnyambu

          16k1522




          16k1522

























              0














              If you're using a flavour of regex that supports negative lookbehind this regex should find the strings you want to replace:



              (?<!/)([A-Za-z]{2}(?!/))



              (?<!/) - makes sure there isn't a / behind the match
              ([A-Za-z]{2} looks for two alphabetic characters
              (?!/)) makes sure there isn't a trailing /



              In Python you would use it like this:



              print(re.sub(r'(?<!/)([A-Za-z]{2}(?!/))',r'1/1','rt bl/ws se gn/wd wk bl/rt'))


              Output:



              rt/rt bl/ws se/se gn/wd wk/wk bl/rt


              Demo on rextester



              In PHP you would use it like so:



              $str = 'rt bl/ws se gn/wd wk bl/rt';
              echo preg_replace('/(?<!/)([A-Za-z]{2}(?!/))/', '$1/$1', $str);


              Output:



              rt/rt bl/ws se/se gn/wd wk/wk bl/rt


              Demo on 3v4l.org






              share|improve this answer






























                0














                If you're using a flavour of regex that supports negative lookbehind this regex should find the strings you want to replace:



                (?<!/)([A-Za-z]{2}(?!/))



                (?<!/) - makes sure there isn't a / behind the match
                ([A-Za-z]{2} looks for two alphabetic characters
                (?!/)) makes sure there isn't a trailing /



                In Python you would use it like this:



                print(re.sub(r'(?<!/)([A-Za-z]{2}(?!/))',r'1/1','rt bl/ws se gn/wd wk bl/rt'))


                Output:



                rt/rt bl/ws se/se gn/wd wk/wk bl/rt


                Demo on rextester



                In PHP you would use it like so:



                $str = 'rt bl/ws se gn/wd wk bl/rt';
                echo preg_replace('/(?<!/)([A-Za-z]{2}(?!/))/', '$1/$1', $str);


                Output:



                rt/rt bl/ws se/se gn/wd wk/wk bl/rt


                Demo on 3v4l.org






                share|improve this answer




























                  0












                  0








                  0







                  If you're using a flavour of regex that supports negative lookbehind this regex should find the strings you want to replace:



                  (?<!/)([A-Za-z]{2}(?!/))



                  (?<!/) - makes sure there isn't a / behind the match
                  ([A-Za-z]{2} looks for two alphabetic characters
                  (?!/)) makes sure there isn't a trailing /



                  In Python you would use it like this:



                  print(re.sub(r'(?<!/)([A-Za-z]{2}(?!/))',r'1/1','rt bl/ws se gn/wd wk bl/rt'))


                  Output:



                  rt/rt bl/ws se/se gn/wd wk/wk bl/rt


                  Demo on rextester



                  In PHP you would use it like so:



                  $str = 'rt bl/ws se gn/wd wk bl/rt';
                  echo preg_replace('/(?<!/)([A-Za-z]{2}(?!/))/', '$1/$1', $str);


                  Output:



                  rt/rt bl/ws se/se gn/wd wk/wk bl/rt


                  Demo on 3v4l.org






                  share|improve this answer















                  If you're using a flavour of regex that supports negative lookbehind this regex should find the strings you want to replace:



                  (?<!/)([A-Za-z]{2}(?!/))



                  (?<!/) - makes sure there isn't a / behind the match
                  ([A-Za-z]{2} looks for two alphabetic characters
                  (?!/)) makes sure there isn't a trailing /



                  In Python you would use it like this:



                  print(re.sub(r'(?<!/)([A-Za-z]{2}(?!/))',r'1/1','rt bl/ws se gn/wd wk bl/rt'))


                  Output:



                  rt/rt bl/ws se/se gn/wd wk/wk bl/rt


                  Demo on rextester



                  In PHP you would use it like so:



                  $str = 'rt bl/ws se gn/wd wk bl/rt';
                  echo preg_replace('/(?<!/)([A-Za-z]{2}(?!/))/', '$1/$1', $str);


                  Output:



                  rt/rt bl/ws se/se gn/wd wk/wk bl/rt


                  Demo on 3v4l.org







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 22 '18 at 7:09

























                  answered Nov 20 '18 at 23:03









                  NickNick

                  33.3k132042




                  33.3k132042






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402349%2fregex-find-strings-without-character%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Hercules Kyvelos

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud