1

I trying to have a regular expression which is finding between two words but those words are not certain one.

2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞASınıfıE10/ENo303

This is my text. I'm trying to find the word between Soyadı and Sınıfı, in this case ERTANĞA, but the word Sınıfı also can be no, numara or any number. This is what I did.

soyad[ıi](.*)S[ıi]n[ıi]f[ıi]|no|numara|[0-9]

[ıi] is for Turkish character issue, don't mind that.

3 Answers 3

2

You can use something like below :

/.*Soyad(ı|i)|S(ı|i)n(ı|i)f(ı|i).*|no.*|numera.*|[0-9]/gmi

Here is the link I worked on : https://regex101.com/r/QXLjLF/1

enter image description here

In JS code:

const regex = /.*Soyad(ı|i)|S(ı|i)n(ı|i)f(ı|i).*|no.*|numera.*|[0-9]/gmi;

var str = `2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞASınıfıE10/ENo303`;


var newStr = str.replace(regex, '');
console.log(newStr);
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry but I have to include insensitive flag also. Why is it returning me all text when I use it with your regex?
@Kaepxer sorry, my bad. didn't see the case insensitive requirement before. Please check now. How does string appear when its numbers after ERTANĞA ? if all are numbers after that above will work, else we need to do little adjustment to remove all numbers after Soyadı
1

You can use a single capture group to get the word ERTANĞA, keep the character class [ıi] instead of using an alternation for (ı|i) and group the alternatives at the end of the pattern using a non capture group (?:

soyad[ıi](.+?)(?:S[ıi]n[ıi]f[ıi]|n(?:o|umara)|[0-9])
  • soyad[ıi] Match soyadı or soyadi
  • (.+?) Capture group 1, match 1 or more chars as least as possible
  • (?: Non capture group
    • S[ıi]n[ıi]f[ıi] Match S and then ı or i etc..
    • | Or
    • n(?:o|umara) Match either no or numara
    • | Or
    • [0-9] Match a digit 0-9
  • ) Close non capture group

Note that you don't need the /m flag as there are no anchors in the pattern.

Regex demo

const regex = /soyad[ıi](.+?)(?:S[ıi]n[ıi]f[ıi]|n(?:o|umara)|[0-9])/gi;
const str = "2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞASınıfıE10/ENo303\n";
console.log(Array.from(str.matchAll(regex), m => m[1]));

Comments

1

This might do it

const str = `2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞASınıfıE10/ENo303
2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞAnumaraE10/ENo303
2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞAnoE10/ENo303`
const re = /(?:Soyad(ı|i))(.*?)(?:S(ı|i)n(ı|i)f(ı|i)|no|numara)/gmi
console.log([...str.matchAll(re)].map(x => x[2]))

ES5

const str = `2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞASınıfıE10/ENo303
2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞAnumaraE10/ENo303
2015ÖĞLEYEMEKKARTI(2016-20.AdıMEVLÜTSoyadıERTANĞAnoE10/ENo303`
const re = /(?:Soyad(ı|i))(.*?)(?:S(ı|i)n(ı|i)f(ı|i)|no|numara)/gmi
const res = []
let match;
while ((match = re.exec(str)) !== null) res.push(match[2])

console.log(res)

4 Comments

My bad, I didn't mentioned but I'm coding in React Native and matchAll() is not working on Android. It needs external library for this, so I'm not prefering this solution until I have no choice.
This is my text. I'm trying to find the word between "Soyadı" and "Sınıfı - this is the answer that will find the word. It is not a replace statement because that is not what you asked for. Replace statement will fail if the string changes
IDK what do you mean by "replace statement will fail if the string changes" but when I try it with different strings It gave me the correct word.
Soyadı means "surname" in Turkish. I'm trying to find surname. So, my bad again not to say that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.